Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. can not print correctly after convert QString to char *
Forum Updated to NodeBB v4.3 + New Features

can not print correctly after convert QString to char *

Scheduled Pinned Locked Moved Solved General and Desktop
28 Posts 8 Posters 4.1k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • V VRonin
    26 Feb 2020, 17:24

    @JonB said in can not print correctly after convert QString to char *:

    Are you saying the s.toUtf8() is returning a temporary, or going .data() is a temporary?

    The former

    J Offline
    J Offline
    JonB
    wrote on 26 Feb 2020, 17:28 last edited by
    #8

    @VRonin
    Fine. So I carefully read https://doc.qt.io/qt-5/qstring.html#toUtf8

    Returns a UTF-8 representation of the string as a QByteArray.

    @Christian-Ehrlicher says the question/code is "C++ basics". I do not see the word "temporary" there. In fact I search the whole of QString doc page and don't find it. So how do I know this, please?

    A 1 Reply Last reply 26 Feb 2020, 17:35
    0
    • J JonB
      26 Feb 2020, 17:28

      @VRonin
      Fine. So I carefully read https://doc.qt.io/qt-5/qstring.html#toUtf8

      Returns a UTF-8 representation of the string as a QByteArray.

      @Christian-Ehrlicher says the question/code is "C++ basics". I do not see the word "temporary" there. In fact I search the whole of QString doc page and don't find it. So how do I know this, please?

      A Offline
      A Offline
      aha_1980
      Lifetime Qt Champion
      wrote on 26 Feb 2020, 17:35 last edited by
      #9

      Hi @JonB,

      as @Christian-Ehrlicher said, that is C++ basics: https://en.cppreference.com/w/cpp/language/lifetime

      Regards

      Qt has to stay free or it will die.

      J 1 Reply Last reply 26 Feb 2020, 17:39
      2
      • A aha_1980
        26 Feb 2020, 17:35

        Hi @JonB,

        as @Christian-Ehrlicher said, that is C++ basics: https://en.cppreference.com/w/cpp/language/lifetime

        Regards

        J Offline
        J Offline
        JonB
        wrote on 26 Feb 2020, 17:39 last edited by JonB
        #10

        @aha_1980
        Wow, OK, yes, I need to read! My problem is I have been "spoiled" by using C# and then Python/PyQt/PySide2 for so long now that I rarely have to think about this!

        So let's take a basic, if my C++ holds up. If I write a function

        QByteArray func()
        {
            QByteArray qb;
            return qb;
        }
        

        does that return such a "temporary object"? And that would be true for any class/struct I decalred and then returned in that fashion?

        A J 2 Replies Last reply 26 Feb 2020, 17:48
        0
        • C Offline
          C Offline
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on 26 Feb 2020, 17:40 last edited by
          #11

          @JonB said in can not print correctly after convert QString to char *:

          does that return such a "temporary object"?

          It's not about returning something. It's about the lifetime of an object.

          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
          Visit the Qt Academy at https://academy.qt.io/catalog

          J 1 Reply Last reply 26 Feb 2020, 17:43
          1
          • C Christian Ehrlicher
            26 Feb 2020, 17:40

            @JonB said in can not print correctly after convert QString to char *:

            does that return such a "temporary object"?

            It's not about returning something. It's about the lifetime of an object.

            J Offline
            J Offline
            JonB
            wrote on 26 Feb 2020, 17:43 last edited by
            #12

            @Christian-Ehrlicher

            Temporary objects are created when a prvalue is materialized so that it can be used as a glvalue, which occurs (since C++17) in the following situations:

            Lovely!

            I also note its second item is:

            returning a prvalue from a function

            Is that where we are here? I'm not stupid, but I am clearly struggling to recognise which situations this applies in.... :(

            1 Reply Last reply
            0
            • J JonB
              26 Feb 2020, 17:39

              @aha_1980
              Wow, OK, yes, I need to read! My problem is I have been "spoiled" by using C# and then Python/PyQt/PySide2 for so long now that I rarely have to think about this!

              So let's take a basic, if my C++ holds up. If I write a function

              QByteArray func()
              {
                  QByteArray qb;
                  return qb;
              }
              

              does that return such a "temporary object"? And that would be true for any class/struct I decalred and then returned in that fashion?

              A Offline
              A Offline
              aha_1980
              Lifetime Qt Champion
              wrote on 26 Feb 2020, 17:48 last edited by
              #13

              Hi @JonB,

              if I take your example and do the following: QByteArray ba = func(); then ba lives until it goes out of scope. But if I do QByteArray hex = func().toHex() I have two conversations in one line. That is no problem here, as I take the result of func() and immediately call toHex() on it. But note that afterward neither the returned value of func() nor of toHex() exists anymore, only hex.

              And that is the whole problem, with data() you access the raw data of an object that's lifetime is already over.

              Regards

              Qt has to stay free or it will die.

              1 Reply Last reply
              1
              • M Mozzie
                26 Feb 2020, 17:01

                Env: vs2017 , Qt 5.14.0, Qt 5.12.6
                code:

                		QString s = "hello world";
                		qDebug() << s;
                		qDebug() << s.toUtf8().data();
                
                		char* p = s.toUtf8().data();
                		qDebug() << p;
                
                		QByteArray b = s.toUtf8();
                		p = b.data();
                		qDebug() << p;
                
                

                output:

                "hello world"
                hello world
                ????????????????????????????????????????????7
                hello world
                

                can somebody explain this, I'd be appreciate

                H Offline
                H Offline
                hskoglund
                wrote on 26 Feb 2020, 17:50 last edited by
                #14

                @Mozzie You had a bit of bad luck, if you compile in Release mode instead of Debug it'll work fine

                "hello world"
                hello world
                hello world
                hello world
                

                And if you switch to MinGW compiler it'll work both in Debug and Release :-)

                J A M 3 Replies Last reply 26 Feb 2020, 17:55
                1
                • C Christian Ehrlicher
                  26 Feb 2020, 17:12

                  @Mozzie said in can not print correctly after convert QString to char *:

                  char* p = s.toUtf8().data();

                  C++ basics - you're creating a temporary here so p points to garbage after this statement.

                  M Offline
                  M Offline
                  Mozzie
                  wrote on 26 Feb 2020, 17:52 last edited by
                  #15

                  @Christian-Ehrlicher
                  Thank you very much, and thank other replyer.
                  I think i understand your reply, and I do fogot the temp object , maybe because I also use java a lot.

                  and i alse have a few questions:

                  1. where is the temp object in memory, stack or heap or somewhere else.
                  2. if it is on stack, it can not remain until the stack is finished
                  C 1 Reply Last reply 26 Feb 2020, 18:35
                  0
                  • H hskoglund
                    26 Feb 2020, 17:50

                    @Mozzie You had a bit of bad luck, if you compile in Release mode instead of Debug it'll work fine

                    "hello world"
                    hello world
                    hello world
                    hello world
                    

                    And if you switch to MinGW compiler it'll work both in Debug and Release :-)

                    J Offline
                    J Offline
                    JonB
                    wrote on 26 Feb 2020, 17:55 last edited by JonB
                    #16

                    @hskoglund
                    Your findings are even more scary in view of the above conversation! :)

                    @aha_1980 , and others
                    I think I get it. Also that it's nothing to do with Qt specific classes. Not because of shared QByteArrays and stuff.

                    So to summarize: s.toUtf8() only "lasts" for the lifetime of the statement (probably rather expression) it is in. But if you go QByteArray b = s.toUtf8() then the b will persist OK as usual. Right?

                    H 1 Reply Last reply 26 Feb 2020, 18:03
                    3
                    • H hskoglund
                      26 Feb 2020, 17:50

                      @Mozzie You had a bit of bad luck, if you compile in Release mode instead of Debug it'll work fine

                      "hello world"
                      hello world
                      hello world
                      hello world
                      

                      And if you switch to MinGW compiler it'll work both in Debug and Release :-)

                      A Offline
                      A Offline
                      aha_1980
                      Lifetime Qt Champion
                      wrote on 26 Feb 2020, 18:01 last edited by
                      #17

                      @hskoglund said in can not print correctly after convert QString to char *:

                      And if you switch to MinGW compiler it'll work both in Debug and Release :-)

                      Today. Tomorrow it will run away with your wife, bankrupt your workplace and aim for world domination.

                      t

                      Qt has to stay free or it will die.

                      1 Reply Last reply
                      4
                      • J JonB
                        26 Feb 2020, 17:55

                        @hskoglund
                        Your findings are even more scary in view of the above conversation! :)

                        @aha_1980 , and others
                        I think I get it. Also that it's nothing to do with Qt specific classes. Not because of shared QByteArrays and stuff.

                        So to summarize: s.toUtf8() only "lasts" for the lifetime of the statement (probably rather expression) it is in. But if you go QByteArray b = s.toUtf8() then the b will persist OK as usual. Right?

                        H Offline
                        H Offline
                        hskoglund
                        wrote on 26 Feb 2020, 18:03 last edited by hskoglund
                        #18

                        Yes! I's just luck that the bits are still around in Release mode. The Debug mode output of ??????? could happen in Release also some other day when the sun doesn't shin.e

                        Anyway, one simple modification to make it waterproof could be:

                        QString s = "hello world";
                        qDebug() << s;
                        qDebug() << s.toUtf8().data();
                        
                        QByteArray a = s.toUtf8();
                        char* p = a.data();
                        qDebug() << p;
                        
                        QByteArray b = s.toUtf8();
                        p = b.data();
                        qDebug() << p;
                        

                        Edit: too fast, didn't read the code in the 3d paragraph ! But they are both waterproof now :-)

                        1 Reply Last reply
                        1
                        • H hskoglund
                          26 Feb 2020, 17:50

                          @Mozzie You had a bit of bad luck, if you compile in Release mode instead of Debug it'll work fine

                          "hello world"
                          hello world
                          hello world
                          hello world
                          

                          And if you switch to MinGW compiler it'll work both in Debug and Release :-)

                          M Offline
                          M Offline
                          Mozzie
                          wrote on 26 Feb 2020, 18:08 last edited by
                          #19

                          @hskoglund
                          that is interesting .
                          i dont have test on linux or MinGW, maybe vs and MinGW is diffrent on deal with temp object?

                          H 1 Reply Last reply 26 Feb 2020, 18:11
                          0
                          • M Mozzie
                            26 Feb 2020, 18:08

                            @hskoglund
                            that is interesting .
                            i dont have test on linux or MinGW, maybe vs and MinGW is diffrent on deal with temp object?

                            H Offline
                            H Offline
                            hskoglund
                            wrote on 26 Feb 2020, 18:11 last edited by
                            #20

                            @Mozzie Actually MinGW works on Windows as well (I prefer it over MSVC2017 because MinGW compiles/builds my projects faster).

                            1 Reply Last reply
                            0
                            • M Mozzie
                              26 Feb 2020, 17:52

                              @Christian-Ehrlicher
                              Thank you very much, and thank other replyer.
                              I think i understand your reply, and I do fogot the temp object , maybe because I also use java a lot.

                              and i alse have a few questions:

                              1. where is the temp object in memory, stack or heap or somewhere else.
                              2. if it is on stack, it can not remain until the stack is finished
                              C Offline
                              C Offline
                              Christian Ehrlicher
                              Lifetime Qt Champion
                              wrote on 26 Feb 2020, 18:35 last edited by
                              #21

                              @Mozzie said in can not print correctly after convert QString to char *:

                              where is the temp object in memory, stack or heap or somewhere else.

                              It's on the stack since you did not allocate it with new

                              if it is on stack, it can not remain until the stack is finished

                              No, this is not allowed since it's unnamed.

                              It's also not c++ specific - you can do the same (in a little bit more obvious way) in C:

                              int *myPtr = nullptr;
                              {
                                int a = 3;
                                myPtr = &a;
                                printf("%d\n", *myPtr);   // works fine
                              }
                              printf("%d\n", *myPtr);   // works on garbage and may eat kitten
                              

                              Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                              Visit the Qt Academy at https://academy.qt.io/catalog

                              J M 2 Replies Last reply 26 Feb 2020, 18:40
                              3
                              • C Christian Ehrlicher
                                26 Feb 2020, 18:35

                                @Mozzie said in can not print correctly after convert QString to char *:

                                where is the temp object in memory, stack or heap or somewhere else.

                                It's on the stack since you did not allocate it with new

                                if it is on stack, it can not remain until the stack is finished

                                No, this is not allowed since it's unnamed.

                                It's also not c++ specific - you can do the same (in a little bit more obvious way) in C:

                                int *myPtr = nullptr;
                                {
                                  int a = 3;
                                  myPtr = &a;
                                  printf("%d\n", *myPtr);   // works fine
                                }
                                printf("%d\n", *myPtr);   // works on garbage and may eat kitten
                                
                                J Offline
                                J Offline
                                JonB
                                wrote on 26 Feb 2020, 18:40 last edited by JonB
                                #22

                                @Christian-Ehrlicher said in can not print correctly after convert QString to char *:

                                int *myPtr = nullptr;

                                Never heard of nullptr in C ;-) NULL was much nicer to read anyway.

                                M 1 Reply Last reply 27 Feb 2020, 09:02
                                0
                                • J JonB
                                  26 Feb 2020, 17:39

                                  @aha_1980
                                  Wow, OK, yes, I need to read! My problem is I have been "spoiled" by using C# and then Python/PyQt/PySide2 for so long now that I rarely have to think about this!

                                  So let's take a basic, if my C++ holds up. If I write a function

                                  QByteArray func()
                                  {
                                      QByteArray qb;
                                      return qb;
                                  }
                                  

                                  does that return such a "temporary object"? And that would be true for any class/struct I decalred and then returned in that fashion?

                                  J Offline
                                  J Offline
                                  jsulm
                                  Lifetime Qt Champion
                                  wrote on 27 Feb 2020, 05:54 last edited by
                                  #23
                                  This post is deleted!
                                  1 Reply Last reply
                                  0
                                  • C Christian Ehrlicher
                                    26 Feb 2020, 18:35

                                    @Mozzie said in can not print correctly after convert QString to char *:

                                    where is the temp object in memory, stack or heap or somewhere else.

                                    It's on the stack since you did not allocate it with new

                                    if it is on stack, it can not remain until the stack is finished

                                    No, this is not allowed since it's unnamed.

                                    It's also not c++ specific - you can do the same (in a little bit more obvious way) in C:

                                    int *myPtr = nullptr;
                                    {
                                      int a = 3;
                                      myPtr = &a;
                                      printf("%d\n", *myPtr);   // works fine
                                    }
                                    printf("%d\n", *myPtr);   // works on garbage and may eat kitten
                                    
                                    M Offline
                                    M Offline
                                    Mozzie
                                    wrote on 27 Feb 2020, 08:51 last edited by
                                    #24

                                    @Christian-Ehrlicher
                                    thanks, it helped a lot.
                                    and i have a hunch

                                    {//main stack
                                    	QString s = "hello world";
                                    	char* p = nullptr;
                                    	{// toUtf8()
                                    		QByteArray b = s.toUtf8();
                                    		{// data();
                                    			p = b.data();
                                    			qDebug() << p; // does this is same as "qDebug() << s.toUtf8().data();"
                                    		}
                                    	}
                                    	// b is freed
                                    	qDebug() << p; // this is same as "char * p = s.toUtf8().data(); qDebug() << p;"
                                    }
                                    

                                    does this right?

                                    C 1 Reply Last reply 27 Feb 2020, 17:02
                                    1
                                    • J JonB
                                      26 Feb 2020, 18:40

                                      @Christian-Ehrlicher said in can not print correctly after convert QString to char *:

                                      int *myPtr = nullptr;

                                      Never heard of nullptr in C ;-) NULL was much nicer to read anyway.

                                      M Offline
                                      M Offline
                                      Mozzie
                                      wrote on 27 Feb 2020, 09:02 last edited by
                                      #25

                                      @JonB
                                      nullptr is a c++11 key word, you can still use NULL, but NULL is defined as 0, sometimes it may cause some problem.

                                      such as:

                                      
                                      void test(int *p)
                                      {
                                      	qDebug() << "int *";
                                      }
                                      void test(int i)
                                      {
                                      	qDebug() << "int";
                                      }
                                      test(NULL);
                                      test(nullptr);
                                      

                                      output

                                      int
                                      int *
                                      
                                      J 1 Reply Last reply 27 Feb 2020, 11:25
                                      1
                                      • M Mozzie
                                        27 Feb 2020, 09:02

                                        @JonB
                                        nullptr is a c++11 key word, you can still use NULL, but NULL is defined as 0, sometimes it may cause some problem.

                                        such as:

                                        
                                        void test(int *p)
                                        {
                                        	qDebug() << "int *";
                                        }
                                        void test(int i)
                                        {
                                        	qDebug() << "int";
                                        }
                                        test(NULL);
                                        test(nullptr);
                                        

                                        output

                                        int
                                        int *
                                        
                                        J Offline
                                        J Offline
                                        JonB
                                        wrote on 27 Feb 2020, 11:25 last edited by JonB
                                        #26

                                        @Mozzie
                                        I know this :) That's why I was picking @Christian-Ehrlicher on his use of nullptr in his C program, it was just intended for amusement ;-)

                                        1 Reply Last reply
                                        1
                                        • M Mozzie
                                          27 Feb 2020, 08:51

                                          @Christian-Ehrlicher
                                          thanks, it helped a lot.
                                          and i have a hunch

                                          {//main stack
                                          	QString s = "hello world";
                                          	char* p = nullptr;
                                          	{// toUtf8()
                                          		QByteArray b = s.toUtf8();
                                          		{// data();
                                          			p = b.data();
                                          			qDebug() << p; // does this is same as "qDebug() << s.toUtf8().data();"
                                          		}
                                          	}
                                          	// b is freed
                                          	qDebug() << p; // this is same as "char * p = s.toUtf8().data(); qDebug() << p;"
                                          }
                                          

                                          does this right?

                                          C Offline
                                          C Offline
                                          Christian Ehrlicher
                                          Lifetime Qt Champion
                                          wrote on 27 Feb 2020, 17:02 last edited by
                                          #27

                                          @Mozzie said in can not print correctly after convert QString to char *:

                                          does this right?

                                          Yes, exactly.

                                          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                                          Visit the Qt Academy at https://academy.qt.io/catalog

                                          M 1 Reply Last reply 27 Feb 2020, 17:28
                                          0

                                          17/28

                                          26 Feb 2020, 18:01

                                          • Login

                                          • Login or register to search.
                                          17 out of 28
                                          • First post
                                            17/28
                                            Last post
                                          0
                                          • Categories
                                          • Recent
                                          • Tags
                                          • Popular
                                          • Users
                                          • Groups
                                          • Search
                                          • Get Qt Extensions
                                          • Unsolved