Memory allocation



  • Hi! I have a question. When we write "SomeClass obj;" in any function's body it means that memory for our class will be allocated on stack. If some fields have a type of a pointer then memory for these fields will be allocated on heap via new or via initialisation list in/of the constructor and cleared in the destructor at the end of lifetime of that class, but the 4(8)*N (N-number of pointer type fields) bytes (size of a pointer at x86 or x86-64) will nevertheless go to the stack. And here is the core of the question. What happens when we write "SomeClass *p = new SomeClass();" but every field in class is a primitive, an array with predefined size or declared using "SomeClass obj;" syntax? How does storaging go on in this case?


  • Moderators

    @Tikani said in Memory allocation:

    SomeClass *p = new SomeClass();

    This is heap allocation. That means the complete object will be stored on the heap - it doesn't matter what the content of the object is - it will be allocated on the heap in this case. Only the pointer (p) will be on the stack.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.