diff --git a/snippet/cpp/test_static.c b/snippet/cpp/test_static.c index a2ca895..4808f23 100644 --- a/snippet/cpp/test_static.c +++ b/snippet/cpp/test_static.c @@ -3,11 +3,18 @@ typedef struct { long long id; - char *name; - char *desc; + char name[30]; + char desc[400]; int age; } Person; +typedef struct { + long long id; + char *title; + char *desc; + int version; +} SmallObj; + void test () { static int num = 0; num++; @@ -24,24 +31,28 @@ void test2() { int main(int argc, char **argv) { // p is on stack, while q is on heap Person p; - Person *q = (Person *)malloc(sizeof(Person)); + Person *q = (Person *) malloc(sizeof(Person)); if (q == NULL) { printf("malloc fails\n"); return -1; } + SmallObj o; + SmallObj *r = (SmallObj *) malloc(sizeof(SmallObj)); + // test(); // test(); // test2(); // test2(); /** - * TODO: 看起来操作系统决定 malloc 申请的内存在哪里 * Output on linux64 compiled by clang - * addr of p: 0x5619048df2d0, addr of q: 0x7fb51f2e19e0 + * addr of p: 0x7ffc94fcaf58, addr of q: 0x565297e022a0, addr of o: 0x7ffc94fcaf38, addr of r: 0x565297e02470 * Output on win64 compiled by MinGW64 gcc * addr of p: 000000B0591FFB20, addr of q: 000000B0591FFB00 */ - printf("addr of p: %p, addr of q: %p\n", p, *q); + printf("addr of p: %p, addr of q: %p, addr of o: %p, addr of r: %p\n", + &p, q, &o, r); free(q); + free(r); return 0; }