Chinaunix首页 | 论坛 | 博客
  • 博客访问: 383329
  • 博文数量: 158
  • 博客积分: 1227
  • 博客等级: 少尉
  • 技术积分: 946
  • 用 户 组: 普通用户
  • 注册时间: 2011-02-20 16:19
文章分类
文章存档

2016年(1)

2015年(1)

2012年(107)

2011年(49)

分类:

2011-06-03 17:25:37

http://blog.csdn.net/yui/archive/2010/11/18/6019715.aspx

这是老问题了,因此写了一个demo,代码如下:

  1. #include   
  2. #include   
  3. #include   
  4. #define MAX_LENGTH 32  
  5. struct node {  
  6.     void *address;  
  7.     char area[16];  
  8.     char desc[128];  
  9. };  
  10. typedef struct node Node;  
  11. Node array[MAX_LENGTH];  
  12. int size = 0;  
  13. void add_item(void *addr, const char *area, const char *desc) {  
  14.     array[size].address = addr;  
  15.     strcpy(array[size].area, area);  
  16.     strcpy(array[size].desc, desc);  
  17.     size++;  
  18. }  
  19. void print_item() {  
  20.     int i;  
  21.     for ( i = 0; i < size; i++ ) {  
  22.         printf("%p\t%s\t%s\n", array[i].address, array[i].area, array[i].desc);  
  23.     }  
  24. }  
  25. void swap_item(Node *a, Node *b) {  
  26.     void *t = (*a).address;  
  27.     (*a).address = (*b).address;  
  28.     (*b).address = t;  
  29.     char s[128];  
  30.     strcpy(s, (*a).area);  
  31.     strcpy((*a).area, (*b).area);  
  32.     strcpy((*b).area, s);  
  33.     strcpy(s, (*a).desc);  
  34.     strcpy((*a).desc, (*b).desc);  
  35.     strcpy((*b).desc, s);  
  36. }  
  37. void sort_item(Node *array, int n) {  
  38.     float factor = 1.3;  
  39.     int g = n;  
  40.     int swapped = 1;  
  41.     while ( g>1 || swapped ) {  
  42.         g = (g>1) ? g/factor : g;  
  43.         swapped = 0;  
  44.         int i = 0;  
  45.         while ( g+i < n ) {  
  46.             if ( array[i].address < array[g+i].address ) {  
  47.                 swap_item(&array[i], &array[g+i]);  
  48.                 swapped = 1;  
  49.             }  
  50.             i++;  
  51.         }  
  52.     }  
  53. }  
  54. char *a() {  
  55.     static int dummya = 0;  
  56.     add_item((void *)&dummya, "STATIC""address of dummy a");  
  57.     printf("a() addr of a() in CODE area  : %p\n", a);  
  58.     add_item((void *)a, "CODE""address of a()");  
  59.     //printf("a() addr of main() is %p\n", main); // this is error, main undeclared  
  60.     char p[] = "hello, everyone!";  
  61.     printf("a() pointing to a STACK area  : %p\n", p);  
  62.     add_item((void *)p, "STACK""address of p in a()");  
  63.     printf("a()     it's content          : %s\n", p);  
  64.     printf("a() addr of the const value   : %p\n""hello, everyone!");  
  65.     return p;  
  66. }  
  67. char *b() {  
  68.     static int dummyb = 0;  
  69.     add_item((void *)&dummyb, "STATIC""address of dummy b");  
  70.     printf("b() addr of b() in CODE area  : %p\n", b);  
  71.     add_item((void *)b, "CODE""address of b()");  
  72.     //printf("b() addr of c()     : %p\n", c);  // this is error, c undeclared  
  73.     char *p = "hello, everyone!";  
  74.     printf("b() pointing to a CONST area : %p\n", p);  
  75.     add_item((void *)p, "CONST""address of p in b()");  
  76.     printf("b()     it's content          : %s\n", p);  
  77.     printf("b() addr of the const value   : %p\n""hello, everyone!");  
  78.     return p;  
  79. }  
  80. char *c() {  
  81.     static int dummyc = 0;  
  82.     add_item((void *)&dummyc, "STATIC""address of dummy c");  
  83.     printf("c() addr of c() in CODE area  : %p\n", c);  
  84.     add_item((void *)c, "CODE""address of c()");  
  85.     char *p = malloc(100);  
  86.     strcpy(p, "hello, everyone!");  
  87.     printf("c() pointing to a HEAP area   : %p\n", p);  
  88.     add_item((void *)p, "HEAP""address of p in c()");  
  89.     printf("c()     it's content          : %s\n", p);  
  90.     printf("c() addr of the const value   : %p\n""hello, everyone!");  
  91.     return p;  
  92. }  
  93. int main()  
  94. {  
  95.     static int dummym = 0;  
  96.     add_item((void *)&dummym, "STATIC""address of dummy main");  
  97.     char *r1 = a();  
  98.     printf("main() addr of a() : %p\n", a);  
  99.     printf("main() pointer recv from a()  : %p\n", r1);  
  100.     printf("main() content recv from a()  : %s\n", r1);  
  101.     printf("-----------------\n");  
  102.     char *r2 = b();  
  103.     printf("main() addr of b() : %p\n", b);  
  104.     printf("main() pointer recv from b()  : %p\n", r2);  
  105.     printf("main() content recv from b()  : %s\n", r2);  
  106.     printf("-----------------\n");  
  107.     char *r3 = c();  
  108.     printf("main() addr of c() : %p\n", c);  
  109.     printf("main() pointer recv from c()  : %p\n", r3);  
  110.     printf("main() content recv from c()  : %s\n", r3);  
  111.     printf("-----------------\n");  
  112.     free(r3);  
  113.     printf("main() addr of the const value: %p\n""hello, everyone!");  
  114.     add_item((void *)"hello, everyone!""CONST""address of const string");  
  115.     printf("main() addr of main() in CODE area : %p\n", main);  
  116.     add_item((void *)main, "CODE""address of main()");  
  117.     add_item((void *)array, "STATIC""address of GLOBAL array");  
  118.     add_item((void *)&size, "STATIC""address of GLOBAL size");  
  119.     printf("-----------------\n");  
  120.     printf("sorted result:\n");  
  121.     int i = 0;  
  122.     add_item((void *)&i, "STACK""address of i in main()");  
  123.     sort_item(array, size);  
  124.     print_item();  
  125.     return 0;  
  126. }  

在各平台的执行结果如下,可以看出各段的位置差异:

Windows 2000

  1. a() addr of a() in CODE area  : 004016B3  
  2. a() pointing to a STACK area  : 0022FEFF  
  3. a()     it's content          : hello, everyone!  
  4. a() addr of the const value   : 00404124  
  5. main() addr of a() : 004016B3  
  6. main() pointer recv from a()  : 0022FEFF  
  7. main() content recv from a()  : w  
  8. -----------------  
  9. b() addr of b() in CODE area  : 0040177D  
  10. b() pointing to a CONST area : 00404124  
  11. b()     it's content          : hello, everyone!  
  12. b() addr of the const value   : 00404124  
  13. main() addr of b() : 0040177D  
  14. main() pointer recv from b()  : 00404124  
  15. main() content recv from b()  : hello, everyone!  
  16. -----------------  
  17. c() addr of c() in CODE area  : 00401830  
  18. c() pointing to a HEAP area   : 00474C40  
  19. c()     it's content          : hello, everyone!  
  20. c() addr of the const value   : 00404124  
  21. main() addr of c() : 00401830  
  22. main() pointer recv from c()  : 00474C40  
  23. main() content recv from c()  : hello, everyone!  
  24. -----------------  
  25. main() addr of the const value: 00404124  
  26. main() addr of main() in CODE area : 00401906  
  27. -----------------  
  28. sorted result:  
  29. 00474C40        HEAP    address of p in c()  
  30. 004060E0        STATIC  address of GLOBAL array  
  31. 00406030        STATIC  address of dummy a  
  32. 0040602C        STATIC  address of dummy b  
  33. 00406028        STATIC  address of dummy c  
  34. 00406024        STATIC  address of dummy main  
  35. 00406020        STATIC  address of GLOBAL size  
  36. 00404124        CONST   address of p in b()  
  37. 00404124        CONST   address of const string  
  38. 00401906        CODE    address of main()  
  39. 00401830        CODE    address of c()  
  40. 0040177D        CODE    address of b()  
  41. 004016B3        CODE    address of a()  
  42. 0022FF40        STACK   address of i in main()  
  43. 0022FEFF        STACK   address of p in a()  

Linux

  1. a() addr of a() in CODE area  : 0x40091d  
  2. a() pointing to a STACK area  : 0x7fff82eaf840  
  3. a()     it's content          : hello, everyone!  
  4. a() addr of the const value   : 0x400edc  
  5. main() addr of a() : 0x40091d  
  6. main() pointer recv from a()  : 0x7fff82eaf840  
  7. main() content recv from a()  : hello, everyone!  
  8. -----------------  
  9. b() addr of b() in CODE area  : 0x4009d4  
  10. b() pointing to a CONST area : 0x400edc  
  11. b()     it's content          : hello, everyone!  
  12. b() addr of the const value   : 0x400edc  
  13. main() addr of b() : 0x4009d4  
  14. main() pointer recv from b()  : 0x400edc  
  15. main() content recv from b()  : hello, everyone!  
  16. -----------------  
  17. c() addr of c() in CODE area  : 0x400a73  
  18. c() pointing to a HEAP area   : 0x1448d010  
  19. c()     it's content          : hello, everyone!  
  20. c() addr of the const value   : 0x400edc  
  21. main() addr of c() : 0x400a73  
  22. main() pointer recv from c()  : 0x1448d010  
  23. main() content recv from c()  : hello, everyone!  
  24. -----------------  
  25. main() addr of the const value: 0x400edc  
  26. main() addr of main() in CODE area : 0x400b3b  
  27. -----------------  
  28. sorted result:  
  29. 0x7fff82eaf874  STACK   address of i in main()  
  30. 0x7fff82eaf840  STACK   address of p in a()  
  31. 0x1448d010      HEAP    address of p in c()  
  32. 0x601700        STATIC  address of GLOBAL array  
  33. 0x6016fc        STATIC  address of dummy main  
  34. 0x6016f8        STATIC  address of dummy c  
  35. 0x6016f4        STATIC  address of dummy b  
  36. 0x6016f0        STATIC  address of dummy a  
  37. 0x6016ec        STATIC  address of GLOBAL size  
  38. 0x400edc        CONST   address of p in b()  
  39. 0x400edc        CONST   address of const string  
  40. 0x400b3b        CODE    address of main()  
  41. 0x400a73        CODE    address of c()  
  42. 0x4009d4        CODE    address of b()  
  43. 0x40091d        CODE    address of a()  

Tru64

  1. a() addr of a() in CODE area  : 120001840  
  2. a() pointing to a STACK area  : 11fffbfa8  
  3. a()     it's content          : hello, everyone!  
  4. a() addr of the const value   : 140000100  
  5. main() addr of a() : 120001840  
  6. main() pointer recv from a()  : 11fffbfa8  
  7. main() content recv from a()  :  
  8. -----------------  
  9. b() addr of b() in CODE area  : 120001950  
  10. b() pointing to a CONST area : 140000168  
  11. b()     it's content          : hello, everyone!  
  12. b() addr of the const value   : 140000210  
  13. main() addr of b() : 120001950  
  14. main() pointer recv from b()  : 140000168  
  15. main() content recv from b()  : hello, everyone!  
  16. -----------------  
  17. c() addr of c() in CODE area  : 120001a40  
  18. c() pointing to a HEAP area   : 140004100  
  19. c()     it's content          : hello, everyone!  
  20. c() addr of the const value   : 140000320  
  21. main() addr of c() : 120001a40  
  22. main() pointer recv from c()  : 140004100  
  23. main() content recv from c()  : hello, everyone!  
  24. -----------------  
  25. main() addr of the const value: 140000510  
  26. main() addr of main() in CODE area : 120001b70  
  27. -----------------  
  28. sorted result:  
  29. 140004100       HEAP    address of p in c()  
  30. 140000870       STATIC  address of GLOBAL array  
  31. 14000072c       STATIC  address of dummy main  
  32. 140000728       STATIC  address of dummy a  
  33. 140000724       STATIC  address of dummy b  
  34. 140000720       STATIC  address of dummy c  
  35. 1400006a0       STATIC  address of GLOBAL size  
  36. 140000528       CONST   address of const string  
  37. 140000168       CONST   address of p in b()  
  38. 120001b70       CODE    address of main()  
  39. 120001a40       CODE    address of c()  
  40. 120001950       CODE    address of b()  
  41. 120001840       CODE    address of a()  
  42. 11fffbfe0       STACK   address of i in main()  
  43. 11fffbfa8       STACK   address of p in a()  

Solaris

  1. a() addr of a() in CODE area  : 10c68  
  2. a() pointing to a STACK area  : ffbfece0  
  3. a()     it's content          : hello, everyone!  
  4. a() addr of the const value   : 112a8  
  5. main() addr of a() : 10c68  
  6. main() pointer recv from a()  : ffbfece0  
  7. main() content recv from a()  : ÿ¿í  
  8. -----------------  
  9. b() addr of b() in CODE area  : 10d64  
  10. b() pointing to a CONST area : 112a8  
  11. b()     it's content          : hello, everyone!  
  12. b() addr of the const value   : 112a8  
  13. main() addr of b() : 10d64  
  14. main() pointer recv from b()  : 112a8  
  15. main() content recv from b()  : hello, everyone!  
  16. -----------------  
  17. c() addr of c() in CODE area  : 10e38  
  18. c() pointing to a HEAP area   : 22c08  
  19. c()     it's content          : hello, everyone!  
  20. c() addr of the const value   : 112a8  
  21. main() addr of c() : 10e38  
  22. main() pointer recv from c()  : 22c08  
  23. main() content recv from c()  : hello, everyone!  
  24. -----------------  
  25. main() addr of the const value: 112a8  
  26. main() addr of main() in CODE area : 10f28  
  27. -----------------  
  28. sorted result:  
  29. ffbfed68        STACK   address of i in main()  
  30. ffbfece0        STACK   address of p in a()  
  31. 22c08   HEAP    address of p in c()  
  32. 2197c   STATIC  address of GLOBAL array  
  33. 21978   STATIC  address of dummy main  
  34. 21974   STATIC  address of dummy c  
  35. 21970   STATIC  address of dummy b  
  36. 2196c   STATIC  address of dummy a  
  37. 21968   STATIC  address of GLOBAL size  
  38. 112a8   CONST   address of p in b()  
  39. 112a8   CONST   address of const string  
  40. 10f28   CODE    address of main()  
  41. 10e38   CODE    address of c()  
  42. 10d64   CODE    address of b()  
  43. 10c68   CODE    address of a()  

HP-UX

  1. a() addr of a() in CODE area  : 777daa80  
  2. a() pointing to a STACK area  : 7fffe7d0  
  3. a()     it's content          : hello, everyone!  
  4. a() addr of the const value   : 40010020  
  5. main() addr of a() : 777daa80  
  6. main() pointer recv from a()  : 7fffe7d0  
  7. main() content recv from a()  :  
  8. -----------------  
  9. b() addr of b() in CODE area  : 777daa90  
  10. b() pointing to a CONST area : 40010040  
  11. b()     it's content          : hello, everyone!  
  12. b() addr of the const value   : 40010060  
  13. main() addr of b() : 777daa90  
  14. main() pointer recv from b()  : 40010040  
  15. main() content recv from b()  : hello, everyone!  
  16. -----------------  
  17. c() addr of c() in CODE area  : 777daaa0  
  18. c() pointing to a HEAP area   : 400157d0  
  19. c()     it's content          : hello, everyone!  
  20. c() addr of the const value   : 40010080  
  21. main() addr of c() : 777daaa0  
  22. main() pointer recv from c()  : 400157d0  
  23. main() content recv from c()  : hello, everyone!  
  24. -----------------  
  25. main() addr of the const value: 400100a0  
  26. main() addr of main() in CODE area : 777daab0  
  27. -----------------  
  28. sorted result:  
  29. 7fffe800        STACK   address of i in main()  
  30. 7fffe7d0        STACK   address of p in a()  
  31. 777daab0        CODE    address of main()  
  32. 777daaa0        CODE    address of c()  
  33. 777daa90        CODE    address of b()  
  34. 777daa80        CODE    address of a()  
  35. 400157d0        HEAP    address of p in c()  
  36. 40010190        STATIC  address of GLOBAL array  
  37. 40010170        STATIC  address of GLOBAL size  
  38. 4001016c        STATIC  address of dummy main  
  39. 40010168        STATIC  address of dummy c  
  40. 40010164        STATIC  address of dummy b  
  41. 40010160        STATIC  address of dummy a  
  42. 400100c0        CONST   address of const string  
  43. 40010040        CONST   address of p in b()  
阅读(1005) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~