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. QStirng memory allocation and free
Forum Update on Monday, May 27th 2025

QStirng memory allocation and free

Scheduled Pinned Locked Moved Solved General and Desktop
qstringmemory allocatidynamic allocat
6 Posts 5 Posters 1.3k 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.
  • L Offline
    L Offline
    Lior
    wrote on 20 Feb 2023, 15:16 last edited by Lior
    #1

    Im confused.
    Let say I have some function

    void someFunction()
    {
        QString str = "Hello, world!";
        //.... 
    }
    

    I understand that it allocation memory on the heap.

    What actually happen in the end of the function, the memory is freed?

    And what will happen if the function is continually called? Is it consider memory leak?

    I understand that is a very basic question, but I don't know where can I read the behavior of typs Qt.

    Tnx

    J M 2 Replies Last reply 20 Feb 2023, 15:35
    0
    • C Offline
      C Offline
      ChrisW67
      wrote on 20 Feb 2023, 19:35 last edited by ChrisW67
      #4

      @Lior Just to avoid confusion, both @jonB and @mpergand are correct, but talking about slightly different things.

      In your code, the storage for the QString object's member variables is allocated on the stack of the function. Internally, QString keeps the content of the string data itself in a memory allocation from the heap* that is entirely managed by the QString object. When the QString goes out of scope, its destructor is called and the heap memory is freed: you have no memory leak.

      Internally, QString will share a buffer of two strings that are assigned equal (or copy constructed):

      QString foo("xyzzy");
      QString bar = foo;
      // "xyzzy" exists in memory only once.  Both QStrings point at it internally
      foo = foo.append("plugh");
      // foo makes itself a deep copy of the buffer it shared with bar and appends "plugh"
      

      This a run-time equivalent of the compiler string sharing.

      • Except, I suspect, in some particular cases like a null string or very short strings.
      S L 2 Replies Last reply 20 Feb 2023, 19:42
      3
      • L Lior
        20 Feb 2023, 15:16

        Im confused.
        Let say I have some function

        void someFunction()
        {
            QString str = "Hello, world!";
            //.... 
        }
        

        I understand that it allocation memory on the heap.

        What actually happen in the end of the function, the memory is freed?

        And what will happen if the function is continually called? Is it consider memory leak?

        I understand that is a very basic question, but I don't know where can I read the behavior of typs Qt.

        Tnx

        J Offline
        J Offline
        JonB
        wrote on 20 Feb 2023, 15:35 last edited by
        #2

        @Lior said in QStirng memory allocation and free:

        What actually happen in the end of the function, the memory is freed?
        And what will happen if the function is continually called? Is it consider memory leak?

        Yes.
        No.

        1 Reply Last reply
        0
        • L Lior
          20 Feb 2023, 15:16

          Im confused.
          Let say I have some function

          void someFunction()
          {
              QString str = "Hello, world!";
              //.... 
          }
          

          I understand that it allocation memory on the heap.

          What actually happen in the end of the function, the memory is freed?

          And what will happen if the function is continually called? Is it consider memory leak?

          I understand that is a very basic question, but I don't know where can I read the behavior of typs Qt.

          Tnx

          M Offline
          M Offline
          mpergand
          wrote on 20 Feb 2023, 16:22 last edited by mpergand
          #3

          @Lior said in QStirng memory allocation and free:

          I understand that it allocation memory on the heap.

          No, str is allocated on the stack and automatically freed at the end of the function.

          But what about "Hello, world!" ?

          const char* p= "Hello, world!";
          
          void someFunction()
          {
              const char* p1= "Hello, world!";
              const char* p2= "Hello, world!";
              qDebug()<<(void*)p<<(void*)p1<<(void*)p2;    
          }
          

          debug shows that the three pointers have the same address.
          The compiler is smart enough to allocate only one emplacement for the three const char strings since they are identical.
          But where are these constants in memory:
          in the data section of the executable and this section persists as long as the program is running.

          1 Reply Last reply
          0
          • C Offline
            C Offline
            ChrisW67
            wrote on 20 Feb 2023, 19:35 last edited by ChrisW67
            #4

            @Lior Just to avoid confusion, both @jonB and @mpergand are correct, but talking about slightly different things.

            In your code, the storage for the QString object's member variables is allocated on the stack of the function. Internally, QString keeps the content of the string data itself in a memory allocation from the heap* that is entirely managed by the QString object. When the QString goes out of scope, its destructor is called and the heap memory is freed: you have no memory leak.

            Internally, QString will share a buffer of two strings that are assigned equal (or copy constructed):

            QString foo("xyzzy");
            QString bar = foo;
            // "xyzzy" exists in memory only once.  Both QStrings point at it internally
            foo = foo.append("plugh");
            // foo makes itself a deep copy of the buffer it shared with bar and appends "plugh"
            

            This a run-time equivalent of the compiler string sharing.

            • Except, I suspect, in some particular cases like a null string or very short strings.
            S L 2 Replies Last reply 20 Feb 2023, 19:42
            3
            • C ChrisW67
              20 Feb 2023, 19:35

              @Lior Just to avoid confusion, both @jonB and @mpergand are correct, but talking about slightly different things.

              In your code, the storage for the QString object's member variables is allocated on the stack of the function. Internally, QString keeps the content of the string data itself in a memory allocation from the heap* that is entirely managed by the QString object. When the QString goes out of scope, its destructor is called and the heap memory is freed: you have no memory leak.

              Internally, QString will share a buffer of two strings that are assigned equal (or copy constructed):

              QString foo("xyzzy");
              QString bar = foo;
              // "xyzzy" exists in memory only once.  Both QStrings point at it internally
              foo = foo.append("plugh");
              // foo makes itself a deep copy of the buffer it shared with bar and appends "plugh"
              

              This a run-time equivalent of the compiler string sharing.

              • Except, I suspect, in some particular cases like a null string or very short strings.
              S Offline
              S Offline
              SGaist
              Lifetime Qt Champion
              wrote on 20 Feb 2023, 19:42 last edited by
              #5

              Hi,

              @ChrisW67 is correct. As for the final point, indeed, there are SSO (short string optimization) that have been implemented in QString to limit the amount of heap allocation.

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              1
              • L Lior has marked this topic as solved on 20 Feb 2023, 21:31
              • C ChrisW67
                20 Feb 2023, 19:35

                @Lior Just to avoid confusion, both @jonB and @mpergand are correct, but talking about slightly different things.

                In your code, the storage for the QString object's member variables is allocated on the stack of the function. Internally, QString keeps the content of the string data itself in a memory allocation from the heap* that is entirely managed by the QString object. When the QString goes out of scope, its destructor is called and the heap memory is freed: you have no memory leak.

                Internally, QString will share a buffer of two strings that are assigned equal (or copy constructed):

                QString foo("xyzzy");
                QString bar = foo;
                // "xyzzy" exists in memory only once.  Both QStrings point at it internally
                foo = foo.append("plugh");
                // foo makes itself a deep copy of the buffer it shared with bar and appends "plugh"
                

                This a run-time equivalent of the compiler string sharing.

                • Except, I suspect, in some particular cases like a null string or very short strings.
                L Offline
                L Offline
                Lior
                wrote on 20 Feb 2023, 21:33 last edited by
                #6

                @ChrisW67 thank you.

                1 Reply Last reply
                0

                1/6

                20 Feb 2023, 15:16

                • Login

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