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 *

can not print correctly after convert QString to char *

Scheduled Pinned Locked Moved Solved General and Desktop
28 Posts 8 Posters 3.2k Views
  • 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.
  • hskoglundH hskoglund

    @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 :-)

    MozzieM Offline
    MozzieM Offline
    Mozzie
    wrote on 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?

    hskoglundH 1 Reply Last reply
    0
    • MozzieM Mozzie

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

      hskoglundH Online
      hskoglundH Online
      hskoglund
      wrote on 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
      • MozzieM Mozzie

        @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
        Christian EhrlicherC Offline
        Christian EhrlicherC Offline
        Christian Ehrlicher
        Lifetime Qt Champion
        wrote on 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

        JonBJ MozzieM 2 Replies Last reply
        3
        • Christian EhrlicherC Christian Ehrlicher

          @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
          
          JonBJ Online
          JonBJ Online
          JonB
          wrote on 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.

          MozzieM 1 Reply Last reply
          0
          • JonBJ JonB

            @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?

            jsulmJ Offline
            jsulmJ Offline
            jsulm
            Lifetime Qt Champion
            wrote on last edited by
            #23
            This post is deleted!
            1 Reply Last reply
            0
            • Christian EhrlicherC Christian Ehrlicher

              @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
              
              MozzieM Offline
              MozzieM Offline
              Mozzie
              wrote on 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?

              Christian EhrlicherC 1 Reply Last reply
              1
              • JonBJ JonB

                @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.

                MozzieM Offline
                MozzieM Offline
                Mozzie
                wrote on 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 *
                
                JonBJ 1 Reply Last reply
                1
                • MozzieM Mozzie

                  @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 *
                  
                  JonBJ Online
                  JonBJ Online
                  JonB
                  wrote on 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
                  • MozzieM Mozzie

                    @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?

                    Christian EhrlicherC Offline
                    Christian EhrlicherC Offline
                    Christian Ehrlicher
                    Lifetime Qt Champion
                    wrote on 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

                    MozzieM 1 Reply Last reply
                    0
                    • Christian EhrlicherC Christian Ehrlicher

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

                      does this right?

                      Yes, exactly.

                      MozzieM Offline
                      MozzieM Offline
                      Mozzie
                      wrote on last edited by
                      #28

                      @Christian-Ehrlicher
                      thanks.

                      1 Reply Last reply
                      0

                      • Login

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