Sub: Update test_static.c

Body:

==== End ====
This commit is contained in:
Shockwave 2023-10-29 18:35:44 +08:00
parent 86fb9a6cf9
commit 9c8833308c
1 changed files with 17 additions and 6 deletions

View File

@ -3,11 +3,18 @@
typedef struct { typedef struct {
long long id; long long id;
char *name; char name[30];
char *desc; char desc[400];
int age; int age;
} Person; } Person;
typedef struct {
long long id;
char *title;
char *desc;
int version;
} SmallObj;
void test () { void test () {
static int num = 0; static int num = 0;
num++; num++;
@ -24,24 +31,28 @@ void test2() {
int main(int argc, char **argv) { int main(int argc, char **argv) {
// p is on stack, while q is on heap // p is on stack, while q is on heap
Person p; Person p;
Person *q = (Person *)malloc(sizeof(Person)); Person *q = (Person *) malloc(sizeof(Person));
if (q == NULL) { if (q == NULL) {
printf("malloc fails\n"); printf("malloc fails\n");
return -1; return -1;
} }
SmallObj o;
SmallObj *r = (SmallObj *) malloc(sizeof(SmallObj));
// test(); // test();
// test(); // test();
// test2(); // test2();
// test2(); // test2();
/** /**
* TODO: malloc
* Output on linux64 compiled by clang * 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 * Output on win64 compiled by MinGW64 gcc
* addr of p: 000000B0591FFB20, addr of q: 000000B0591FFB00 * 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(q);
free(r);
return 0; return 0;
} }