QStirng memory allocation and free
-
Im confused.
Let say I have some functionvoid 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
-
@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.
-
@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. -
@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.
-