
Originally Posted by
lni
I can't see any connection between that article and things you are saying :P
As for me it's simple:
If there's code like this:
A a;
A a;
To copy to clipboard, switch view to plain text mode
object a of type A is created on stack. If that code appears as a local variable in some function/method then at the end of the function/method object a will be deleted from stack, because program is returning to the caller, to place where call appeared.
(that's why there's stack overflow in infinite recursion -> infinite storing the called method variables and no returns, which will remove these variables from stack)
But if there's code like this:
A *a = new A;
A *a = new A;
To copy to clipboard, switch view to plain text mode
instead of previously shown, then it's different story :] it means that your local variable created on a stack is a pointer (32-bit address = 4B) which points to the object of type A created on heap - not afecting stack in any way.
So if there is an array in struct created dynamically on a heap:
struct myStruc {
int *array;
myStruc() {
array = new int[ 3000*2000 ];
}
};
struct myStruc {
QString myString;
int *array;
myStruc() {
array = new int[ 3000*2000 ];
}
};
To copy to clipboard, switch view to plain text mode
then this struct size is just a size of myString and size of pointer to an array. An array itself is always on heap (beacause of operator new).
If there is any mistake in my understanding of stack/heap, feel free to provide any interesting links on this topic.
Bookmarks