
Originally Posted by
lni
FALSE.
Try this and tell me why it works.
:-)
Explain this:
#include "stdio.h"
#include <QString>
struct A {
int array[1000];
};
int main(){
A a;
A *b = new A;
printf("Addr of a: 0x%08x\n", (unsigned int)&a);
printf("Addr of a.array: 0x%08x\n", (unsigned int)a.array);
printf("Addr of b: 0x%08x\n", (unsigned int)b);
printf("Addr of b->array: 0x%08x\n", (unsigned int)b->array);
return 0;
}
#include "stdio.h"
#include <QString>
struct A {
QString x;
int array[1000];
};
int main(){
A a;
A *b = new A;
printf("Addr of a: 0x%08x\n", (unsigned int)&a);
printf("Addr of a.array: 0x%08x\n", (unsigned int)a.array);
printf("Addr of b: 0x%08x\n", (unsigned int)b);
printf("Addr of b->array: 0x%08x\n", (unsigned int)b->array);
return 0;
}
To copy to clipboard, switch view to plain text mode
Result:
Addr of a: 0xbfa5008c
Addr of a.array: 0xbfa50090
Addr of b: 0x089fe1b8
Addr of b->array: 0x089fe1bc
Addr of a: 0xbfa5008c
Addr of a.array: 0xbfa50090
Addr of b: 0x089fe1b8
Addr of b->array: 0x089fe1bc
To copy to clipboard, switch view to plain text mode
As you see a and a.array are on the stack and b and b->array are on heap.
Your "example" works because even if "myStruc" is allocated on the stack, its array member will be allocated on the heap. But it doesn't mean that if myStruc is allocated on the heap, array is (or can be) allocated on the stack
Bookmarks