http://blog.csdn.net/yui/archive/2010/11/18/6019715.aspx
这是老问题了,因此写了一个demo,代码如下:
- #include
- #include
- #include
- #define MAX_LENGTH 32
- struct node {
- void *address;
- char area[16];
- char desc[128];
- };
- typedef struct node Node;
- Node array[MAX_LENGTH];
- int size = 0;
- void add_item(void *addr, const char *area, const char *desc) {
- array[size].address = addr;
- strcpy(array[size].area, area);
- strcpy(array[size].desc, desc);
- size++;
- }
- void print_item() {
- int i;
- for ( i = 0; i < size; i++ ) {
- printf("%p\t%s\t%s\n", array[i].address, array[i].area, array[i].desc);
- }
- }
- void swap_item(Node *a, Node *b) {
- void *t = (*a).address;
- (*a).address = (*b).address;
- (*b).address = t;
- char s[128];
- 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[i].address < array[g+i].address ) {
- swap_item(&array[i], &array[g+i]);
- 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()");
-
- 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()");
-
- 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 : 004016B3
- a() pointing to a STACK area : 0022FEFF
- a() it's content : hello, everyone!
- a() addr of the const value : 00404124
- main() addr of a() : 004016B3
- main() pointer recv from a() : 0022FEFF
- main() content recv from a() : w
- -----------------
- b() addr of b() in CODE area : 0040177D
- b() pointing to a CONST area : 00404124
- b() it's content : hello, everyone!
- b() addr of the const value : 00404124
- main() addr of b() : 0040177D
- main() pointer recv from b() : 00404124
- main() content recv from b() : hello, everyone!
- -----------------
- c() addr of c() in CODE area : 00401830
- c() pointing to a HEAP area : 00474C40
- c() it's content : hello, everyone!
- c() addr of the const value : 00404124
- main() addr of c() : 00401830
- main() pointer recv from c() : 00474C40
- main() content recv from c() : hello, everyone!
- -----------------
- main() addr of the const value: 00404124
- main() addr of main() in CODE area : 00401906
- -----------------
- sorted result:
- 00474C40 HEAP address of p in c()
- 004060E0 STATIC address of GLOBAL array
- 00406030 STATIC address of dummy a
- 0040602C STATIC address of dummy b
- 00406028 STATIC address of dummy c
- 00406024 STATIC address of dummy main
- 00406020 STATIC address of GLOBAL size
- 00404124 CONST address of p in b()
- 00404124 CONST address of const string
- 00401906 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 : 0x40091d
- a() pointing to a STACK area : 0x7fff82eaf840
- a() it's content : hello, everyone!
- a() addr of the const value : 0x400edc
- main() addr of a() : 0x40091d
- main() pointer recv from a() : 0x7fff82eaf840
- main() content recv from a() : hello, everyone!
- -----------------
- b() addr of b() in CODE area : 0x4009d4
- b() pointing to a CONST area : 0x400edc
- b() it's content : hello, everyone!
- b() addr of the const value : 0x400edc
- main() addr of b() : 0x4009d4
- main() pointer recv from b() : 0x400edc
- main() content recv from b() : hello, everyone!
- -----------------
- c() addr of c() in CODE area : 0x400a73
- c() pointing to a HEAP area : 0x1448d010
- c() it's content : hello, everyone!
- c() addr of the const value : 0x400edc
- main() addr of c() : 0x400a73
- main() pointer recv from c() : 0x1448d010
- main() content recv from c() : hello, everyone!
- -----------------
- main() addr of the const value: 0x400edc
- main() addr of main() in CODE area : 0x400b3b
- -----------------
- sorted result:
- 0x7fff82eaf874 STACK address of i in main()
- 0x7fff82eaf840 STACK address of p in a()
- 0x1448d010 HEAP address of p in c()
- 0x601700 STATIC address of GLOBAL array
- 0x6016fc STATIC address of dummy main
- 0x6016f8 STATIC address of dummy c
- 0x6016f4 STATIC address of dummy b
- 0x6016f0 STATIC address of dummy a
- 0x6016ec STATIC address of GLOBAL size
- 0x400edc CONST address of p in b()
- 0x400edc CONST address of const string
- 0x400b3b 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 : 120001840
- a() pointing to a STACK area : 11fffbfa8
- a() it's content : hello, everyone!
- a() addr of the const value : 140000100
- main() addr of a() : 120001840
- main() pointer recv from a() : 11fffbfa8
- main() content recv from a() :
- -----------------
- b() addr of b() in CODE area : 120001950
- b() pointing to a CONST area : 140000168
- b() it's content : hello, everyone!
- b() addr of the const value : 140000210
- main() addr of b() : 120001950
- main() pointer recv from b() : 140000168
- main() content recv from b() : hello, everyone!
- -----------------
- c() addr of c() in CODE area : 120001a40
- c() pointing to a HEAP area : 140004100
- c() it's content : hello, everyone!
- c() addr of the const value : 140000320
- main() addr of c() : 120001a40
- main() pointer recv from c() : 140004100
- main() content recv from c() : hello, everyone!
- -----------------
- main() addr of the const value: 140000510
- main() addr of main() in CODE area : 120001b70
- -----------------
- sorted result:
- 140004100 HEAP address of p in c()
- 140000870 STATIC address of GLOBAL array
- 14000072c STATIC address of dummy main
- 140000728 STATIC address of dummy a
- 140000724 STATIC address of dummy b
- 140000720 STATIC address of dummy c
- 1400006a0 STATIC address of GLOBAL size
- 140000528 CONST address of const string
- 140000168 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 : 10c68
- a() pointing to a STACK area : ffbfece0
- a() it's content : hello, everyone!
- a() addr of the const value : 112a8
- main() addr of a() : 10c68
- main() pointer recv from a() : ffbfece0
- main() content recv from a() : ÿ¿í
- -----------------
- b() addr of b() in CODE area : 10d64
- b() pointing to a CONST area : 112a8
- b() it's content : hello, everyone!
- b() addr of the const value : 112a8
- main() addr of b() : 10d64
- main() pointer recv from b() : 112a8
- main() content recv from b() : hello, everyone!
- -----------------
- c() addr of c() in CODE area : 10e38
- c() pointing to a HEAP area : 22c08
- c() it's content : hello, everyone!
- c() addr of the const value : 112a8
- main() addr of c() : 10e38
- main() pointer recv from c() : 22c08
- main() content recv from c() : hello, everyone!
- -----------------
- main() addr of the const value: 112a8
- main() 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 STATIC address of GLOBAL array
- 21978 STATIC address of dummy main
- 21974 STATIC address of dummy c
- 21970 STATIC address of dummy b
- 2196c STATIC address of dummy a
- 21968 STATIC address of GLOBAL size
- 112a8 CONST address of p in b()
- 112a8 CONST address of const string
- 10f28 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 : 777daa80
- a() pointing to a STACK area : 7fffe7d0
- a() it's content : hello, everyone!
- a() addr of the const value : 40010020
- main() addr of a() : 777daa80
- main() pointer recv from a() : 7fffe7d0
- main() content recv from a() :
- -----------------
- b() addr of b() in CODE area : 777daa90
- b() pointing to a CONST area : 40010040
- b() it's content : hello, everyone!
- b() addr of the const value : 40010060
- main() addr of b() : 777daa90
- main() pointer recv from b() : 40010040
- main() content recv from b() : hello, everyone!
- -----------------
- c() addr of c() in CODE area : 777daaa0
- c() pointing to a HEAP area : 400157d0
- c() it's content : hello, everyone!
- c() addr of the const value : 40010080
- main() addr of c() : 777daaa0
- main() pointer recv from c() : 400157d0
- main() content recv from c() : hello, everyone!
- -----------------
- main() addr of the const value: 400100a0
- main() 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 STATIC address of GLOBAL array
- 40010170 STATIC address of GLOBAL size
- 4001016c STATIC address of dummy main
- 40010168 STATIC address of dummy c
- 40010164 STATIC address of dummy b
- 40010160 STATIC address of dummy a
- 400100c0 CONST address of const string
- 40010040 CONST address of p in b()
阅读(407) | 评论(0) | 转发(0) |