栈区、堆区、静态区、常量区、代码区演示
这是老问题了,今天被同事问到,因此写了一个demo,代码如下:#include <stdio.h>#include <string.h>#include <stdlib.h>#define MAX_LENGTH 32struct node { void *address; char area; char desc;};typedef struct node Node;Node array;int size = 0;void add_item(void *addr, const char *area, const char *desc) { array.address = addr; strcpy(array.area, area); strcpy(array.desc, desc); size++;}void print_item() { int i; for ( i = 0; i < size; i++ ) { printf("%p\t%s\t%s\n", array.address, array.area, array.desc); }}void swap_item(Node *a, Node *b) { void *t = (*a).address; (*a).address = (*b).address; (*b).address = t; char s; strcpy(s, (*a).area); strcpy((*a).area, (*b).area); strcpy((*b).area, s); strcpy(s, (*a).desc); strcpy((*a).desc, (*b).desc); strcpy((*b).desc, s);}void sort_item(Node *array, int n) { float factor = 1.3; int g = n; int swapped = 1; while ( g>1 || swapped ) { g = (g>1) ? g/factor : g; swapped = 0; int i = 0; while ( g+i < n ) { if ( array.address < array.address ) { swap_item(&array, &array); swapped = 1; } i++; } }}char *a() { static int dummya = 0; add_item((void *)&dummya, "STATIC", "address of dummy a"); printf("a() addr of a() in CODE area: %p\n", a); add_item((void *)a, "CODE", "address of a()"); //printf("a() addr of main() is %p\n", main); // this is error, main undeclared char p[] = "hello, everyone!"; printf("a() pointing to a STACK area: %p\n", p); add_item((void *)p, "STACK", "address of p in a()"); printf("a() it's content : %s\n", p); printf("a() addr of the const value : %p\n", "hello, everyone!"); return p;}char *b() { static int dummyb = 0; add_item((void *)&dummyb, "STATIC", "address of dummy b"); printf("b() addr of b() in CODE area: %p\n", b); add_item((void *)b, "CODE", "address of b()"); //printf("b() addr of c() : %p\n", c);// this is error, c undeclared char *p = "hello, everyone!"; printf("b() pointing to a CONST area : %p\n", p); add_item((void *)p, "CONST", "address of p in b()"); printf("b() it's content : %s\n", p); printf("b() addr of the const value : %p\n", "hello, everyone!"); return p;}char *c() { static int dummyc = 0; add_item((void *)&dummyc, "STATIC", "address of dummy c"); printf("c() addr of c() in CODE area: %p\n", c); add_item((void *)c, "CODE", "address of c()"); char *p = malloc(100); strcpy(p, "hello, everyone!"); printf("c() pointing to a HEAP area : %p\n", p); add_item((void *)p, "HEAP", "address of p in c()"); printf("c() it's content : %s\n", p); printf("c() addr of the const value : %p\n", "hello, everyone!"); return p;}int main(){ static int dummym = 0; add_item((void *)&dummym, "STATIC", "address of dummy main"); char *r1 = a(); printf("main() addr of a() : %p\n", a); printf("main() pointer recv from a(): %p\n", r1); printf("main() content recv from a(): %s\n", r1); printf("-----------------\n"); char *r2 = b(); printf("main() addr of b() : %p\n", b); printf("main() pointer recv from b(): %p\n", r2); printf("main() content recv from b(): %s\n", r2); printf("-----------------\n"); char *r3 = c(); printf("main() addr of c() : %p\n", c); printf("main() pointer recv from c(): %p\n", r3); printf("main() content recv from c(): %s\n", r3); printf("-----------------\n"); free(r3); printf("main() addr of the const value: %p\n", "hello, everyone!"); add_item((void *)"hello, everyone!", "CONST", "address of const string"); printf("main() addr of main() in CODE area : %p\n", main); add_item((void *)main, "CODE", "address of main()"); add_item((void *)array, "STATIC", "address of GLOBAL array"); add_item((void *)&size, "STATIC", "address of GLOBAL size"); printf("-----------------\n"); printf("sorted result:\n"); int i = 0; add_item((void *)&i, "STACK", "address of i in main()"); sort_item(array, size); print_item(); return 0;}
在各平台的执行结果如下,可以看出各段的位置差异:
Windows 2000
a() addr of a() in CODE area: 004016B3a() pointing to a STACK area: 0022FEFFa() it's content : hello, everyone!a() addr of the const value : 00404124main() addr of a() : 004016B3main() pointer recv from a(): 0022FEFFmain() content recv from a(): w-----------------b() addr of b() in CODE area: 0040177Db() pointing to a CONST area : 00404124b() it's content : hello, everyone!b() addr of the const value : 00404124main() addr of b() : 0040177Dmain() pointer recv from b(): 00404124main() content recv from b(): hello, everyone!-----------------c() addr of c() in CODE area: 00401830c() pointing to a HEAP area : 00474C40c() it's content : hello, everyone!c() addr of the const value : 00404124main() addr of c() : 00401830main() pointer recv from c(): 00474C40main() content recv from c(): hello, everyone!-----------------main() addr of the const value: 00404124main() addr of main() in CODE area : 00401906-----------------sorted result:00474C40 HEAP address of p in c()004060E0 STATICaddress of GLOBAL array00406030 STATICaddress of dummy a0040602C STATICaddress of dummy b00406028 STATICaddress of dummy c00406024 STATICaddress of dummy main00406020 STATICaddress of GLOBAL size00404124 CONST address of p in b()00404124 CONST address of const string00401906 CODE address of main()00401830 CODE address of c()0040177D CODE address of b()004016B3 CODE address of a()0022FF40 STACK address of i in main()0022FEFF STACK address of p in a()
Linux
a() addr of a() in CODE area: 0x40091da() pointing to a STACK area: 0x7fff82eaf840a() it's content : hello, everyone!a() addr of the const value : 0x400edcmain() addr of a() : 0x40091dmain() pointer recv from a(): 0x7fff82eaf840main() content recv from a(): hello, everyone!-----------------b() addr of b() in CODE area: 0x4009d4b() pointing to a CONST area : 0x400edcb() it's content : hello, everyone!b() addr of the const value : 0x400edcmain() addr of b() : 0x4009d4main() pointer recv from b(): 0x400edcmain() content recv from b(): hello, everyone!-----------------c() addr of c() in CODE area: 0x400a73c() pointing to a HEAP area : 0x1448d010c() it's content : hello, everyone!c() addr of the const value : 0x400edcmain() addr of c() : 0x400a73main() pointer recv from c(): 0x1448d010main() content recv from c(): hello, everyone!-----------------main() addr of the const value: 0x400edcmain() addr of main() in CODE area : 0x400b3b-----------------sorted result:0x7fff82eaf874STACK address of i in main()0x7fff82eaf840STACK address of p in a()0x1448d010 HEAP address of p in c()0x601700 STATICaddress of GLOBAL array0x6016fc STATICaddress of dummy main0x6016f8 STATICaddress of dummy c0x6016f4 STATICaddress of dummy b0x6016f0 STATICaddress of dummy a0x6016ec STATICaddress of GLOBAL size0x400edc CONST address of p in b()0x400edc CONST address of const string0x400b3b CODE address of main()0x400a73 CODE address of c()0x4009d4 CODE address of b()0x40091d CODE address of a()
Tru64
a() addr of a() in CODE area: 120001840a() pointing to a STACK area: 11fffbfa8a() it's content : hello, everyone!a() addr of the const value : 140000100main() addr of a() : 120001840main() pointer recv from a(): 11fffbfa8main() content recv from a():-----------------b() addr of b() in CODE area: 120001950b() pointing to a CONST area : 140000168b() it's content : hello, everyone!b() addr of the const value : 140000210main() addr of b() : 120001950main() pointer recv from b(): 140000168main() content recv from b(): hello, everyone!-----------------c() addr of c() in CODE area: 120001a40c() pointing to a HEAP area : 140004100c() it's content : hello, everyone!c() addr of the const value : 140000320main() addr of c() : 120001a40main() pointer recv from c(): 140004100main() content recv from c(): hello, everyone!-----------------main() addr of the const value: 140000510main() addr of main() in CODE area : 120001b70-----------------sorted result:140004100 HEAP address of p in c()140000870 STATICaddress of GLOBAL array14000072c STATICaddress of dummy main140000728 STATICaddress of dummy a140000724 STATICaddress of dummy b140000720 STATICaddress of dummy c1400006a0 STATICaddress of GLOBAL size140000528 CONST address of const string140000168 CONST address of p in b()120001b70 CODE address of main()120001a40 CODE address of c()120001950 CODE address of b()120001840 CODE address of a()11fffbfe0 STACK address of i in main()11fffbfa8 STACK address of p in a()
Solaris
a() addr of a() in CODE area: 10c68a() pointing to a STACK area: ffbfece0a() it's content : hello, everyone!a() addr of the const value : 112a8main() addr of a() : 10c68main() pointer recv from a(): ffbfece0main() content recv from a(): ÿ¿í-----------------b() addr of b() in CODE area: 10d64b() pointing to a CONST area : 112a8b() it's content : hello, everyone!b() addr of the const value : 112a8main() addr of b() : 10d64main() pointer recv from b(): 112a8main() content recv from b(): hello, everyone!-----------------c() addr of c() in CODE area: 10e38c() pointing to a HEAP area : 22c08c() it's content : hello, everyone!c() addr of the const value : 112a8main() addr of c() : 10e38main() pointer recv from c(): 22c08main() content recv from c(): hello, everyone!-----------------main() addr of the const value: 112a8main() addr of main() in CODE area : 10f28-----------------sorted result:ffbfed68 STACK address of i in main()ffbfece0 STACK address of p in a()22c08 HEAP address of p in c()2197c STATICaddress of GLOBAL array21978 STATICaddress of dummy main21974 STATICaddress of dummy c21970 STATICaddress of dummy b2196c STATICaddress of dummy a21968 STATICaddress of GLOBAL size112a8 CONST address of p in b()112a8 CONST address of const string10f28 CODE address of main()10e38 CODE address of c()10d64 CODE address of b()10c68 CODE address of a()
HP-UX
a() addr of a() in CODE area: 777daa80a() pointing to a STACK area: 7fffe7d0a() it's content : hello, everyone!a() addr of the const value : 40010020main() addr of a() : 777daa80main() pointer recv from a(): 7fffe7d0main() content recv from a():-----------------b() addr of b() in CODE area: 777daa90b() pointing to a CONST area : 40010040b() it's content : hello, everyone!b() addr of the const value : 40010060main() addr of b() : 777daa90main() pointer recv from b(): 40010040main() content recv from b(): hello, everyone!-----------------c() addr of c() in CODE area: 777daaa0c() pointing to a HEAP area : 400157d0c() it's content : hello, everyone!c() addr of the const value : 40010080main() addr of c() : 777daaa0main() pointer recv from c(): 400157d0main() content recv from c(): hello, everyone!-----------------main() addr of the const value: 400100a0main() addr of main() in CODE area : 777daab0-----------------sorted result:7fffe800 STACK address of i in main()7fffe7d0 STACK address of p in a()777daab0 CODE address of main()777daaa0 CODE address of c()777daa90 CODE address of b()777daa80 CODE address of a()400157d0 HEAP address of p in c()40010190 STATICaddress of GLOBAL array40010170 STATICaddress of GLOBAL size4001016c STATICaddress of dummy main40010168 STATICaddress of dummy c40010164 STATICaddress of dummy b40010160 STATICaddress of dummy a400100c0 CONST address of const string40010040 CONST address of p in b()
页:
[1]