59 lines
1.3 KiB
C
59 lines
1.3 KiB
C
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
|
|
typedef struct {
|
|
long long id;
|
|
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++;
|
|
printf("Function test has been called %d times\n", num);
|
|
}
|
|
|
|
void test2() {
|
|
// static variables defined in fuction can only be visited by this function.
|
|
static int num = 0;
|
|
num++;
|
|
printf("Function test2 has been called %d times\n", num);
|
|
}
|
|
|
|
int main(int argc, char **argv) {
|
|
// p is on stack, while q is on heap
|
|
Person p;
|
|
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();
|
|
/**
|
|
* Output on linux64 compiled by clang
|
|
* 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, addr of o: %p, addr of r: %p\n",
|
|
&p, q, &o, r);
|
|
free(q);
|
|
free(r);
|
|
return 0;
|
|
}
|