Originally Posted by
lni
Honestly I don't see any connection between the issue of the array being on the stack or not and the article. I think the example I gave you is a proof that can't be undermined.
Stack addresses have high values because in most (all?) modern architectures the stack expands "downwards" (to lower memory addresses) so there has to be enough space for the stack to expand until it reaches the heap space (that is (usually?) between the global variable address space and the stack address space). The example clearly shows that a.array is positioned 4 bytes away from the beginning of the structure in the same adress region so it has to be inside the struct as sizeof(A) will return a value larger than 4 bytes so a logical conclusion follows that both "objects" have to be positioned in the same "part" of memory. The same goes for the second variant, the difference is also 4 bytes and the size of the structure remains the same.
Here is a result of the extended version of my previous example. Note the difference in structure sizes and addresses.
Addr of a: 0xbfcaebb0
Addr of a.array: 0xbfcaebb4
Addr of b: 0x0808d1b8
Addr of b->array: 0x0808d1bc
Size of struct A: 4004
Addr of c: 0xbfcafb54
Addr of c.array: 0x0808e168
Addr of d: 0x0808f110
Addr of d->array: 0x0808f120
Size of struct B: 8
Addr of a: 0xbfcaebb0
Addr of a.array: 0xbfcaebb4
Addr of b: 0x0808d1b8
Addr of b->array: 0x0808d1bc
Size of struct A: 4004
Addr of c: 0xbfcafb54
Addr of c.array: 0x0808e168
Addr of d: 0x0808f110
Addr of d->array: 0x0808f120
Size of struct B: 8
To copy to clipboard, switch view to plain text mode
And the code itself...
#include "stdio.h"
#include <QString>
struct A {
int array[1000];
};
struct B {
int *array;
B(){ array = new int[1000]; }
};
int main(){
A a;
A *b = new A;
B c;
B *d = new B;
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);
printf("Size of struct A: %d\n", sizeof(A));
printf("\n");
printf("Addr of c: 0x%08x\n", (unsigned int)&c);
printf("Addr of c.array: 0x%08x\n", (unsigned int)c.array);
printf("Addr of d: 0x%08x\n", (unsigned int)d);
printf("Addr of d->array: 0x%08x\n", (unsigned int)d->array);
printf("Size of struct B: %d\n", sizeof(B));
return 0;
}
#include "stdio.h"
#include <QString>
struct A {
QString x;
int array[1000];
};
struct B {
QString x;
int *array;
B(){ array = new int[1000]; }
};
int main(){
A a;
A *b = new A;
B c;
B *d = new B;
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);
printf("Size of struct A: %d\n", sizeof(A));
printf("\n");
printf("Addr of c: 0x%08x\n", (unsigned int)&c);
printf("Addr of c.array: 0x%08x\n", (unsigned int)c.array);
printf("Addr of d: 0x%08x\n", (unsigned int)d);
printf("Addr of d->array: 0x%08x\n", (unsigned int)d->array);
printf("Size of struct B: %d\n", sizeof(B));
return 0;
}
To copy to clipboard, switch view to plain text mode
Bookmarks