Chinaunix首页 | 论坛 | 博客
  • 博客访问: 9637237
  • 博文数量: 1293
  • 博客积分: 13501
  • 博客等级: 上将
  • 技术积分: 17974
  • 用 户 组: 普通用户
  • 注册时间: 2011-03-08 18:11
文章分类

全部博文(1293)

文章存档

2019年(1)

2018年(1)

2016年(118)

2015年(257)

2014年(128)

2013年(222)

2012年(229)

2011年(337)

分类: PHP

2011-12-02 15:10:34

function.h
  1. #ifndef _FUNCTION_H_
  2. #define _FUNCTION_H_


  3. void test_extern1_print();
  4. void test_extern2_print();

  5. #endif
header.h
  1. #ifndef _HEADDER_H_
  2. #define _HEADDER_H_

  3. extern int test_extern1_val;
  4. extern int test_extern2_val;
  5. static int tmp_vale = 3;

  6. #endif
 
test_extern1.c
  1. #include <stdio.h>
  2. #include "header.h"

  3. int test_extern1_val = 1;

  4. void test_extern1_print()
  5. {
  6.     printf("In test_extern1.c:\n");
  7.     printf("______________________________________________________________________________________________\n");
  8.     printf("| test_extern1_val | address | test_extern2_val | address | tmp_vale | address |\n");
  9.     printf("______________________________________________________________________________________________\n");
  10.     printf("| %d | %p | %d | %p | %d | %p |\n",\
  11.            test_extern1_val,&test_extern1_val,test_extern2_val,&test_extern2_val,tmp_vale,&tmp_vale);
  12.     printf("______________________________________________________________________________________________\n");
  13. }
 
test_extern2.c
  1. #include <stdio.h>
  2. #include "header.h"

  3. int test_extern2_val = 2;

  4. void test_extern2_print()
  5. {
  6.     printf("In test_extern2.c:\n");
  7.     printf("______________________________________________________________________________________________\n");
  8.     printf("| test_extern1_val | address | test_extern2_val | address | tmp_vale | address |\n");
  9.     printf("______________________________________________________________________________________________\n");
  10.     printf("| %d | %p | %d | %p | %d | %p |\n",\
  11.            test_extern1_val,&test_extern1_val,test_extern2_val,&test_extern2_val,tmp_vale,&tmp_vale);
  12.     printf("______________________________________________________________________________________________\n");
  13. }
 
main.c
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include "founctions.h"
  4. #include "header.h"


  5. int main()
  6. {
  7.     system("clear");
  8.     printf("\n\n****************************************** Head *****************************************\n\n");
  9.     test_extern1_print();
  10.     printf("\n\n");
  11.     test_extern2_print();
  12.     printf("\n\n");
  13.     printf("In main.c:\n");
  14.     printf("______________________________________________________________________________________________\n");
  15.     printf("| test_extern1_val | address | test_extern2_val | address | tmp_vale | address |\n");
  16.     printf("______________________________________________________________________________________________\n");
  17.     printf("| %d | %p | %d | %p | %d | %p |\n",\
  18.            test_extern1_val,&test_extern1_val,test_extern2_val,&test_extern2_val,tmp_vale,&tmp_vale);
  19.     printf("______________________________________________________________________________________________\n");
  20.     printf("\n\n");
  21.     printf("\n\n****************************************** Tail *****************************************\n\n");
  22.     return 0;
  23. }
Makefile
  1. o_file := test_extern1.o test_extern2.o

  2. all: main
  3. main : main.o $(o_file)
  4.     $(CC) main.o $(o_file) -o main

  5. clean:
  6.     rm -rvf ./*.o main
 
编译效果:
  1. ****************************************** Head *****************************************

  2. In test_extern1.c:
  3. ______________________________________________________________________________________
  4. | test_extern1_val | address   | test_extern2_val | address   | tmp_vale | address   |
  5. ______________________________________________________________________________________
  6. |          1       | 0x8049ca0 |          2       | 0x8049ca8 |      3   | 0x8049c9c |
  7. ______________________________________________________________________________________


  8. In test_extern2.c:
  9. ______________________________________________________________________________________
  10. | test_extern1_val | address   | test_extern2_val | address   | tmp_vale | address   |
  11. ______________________________________________________________________________________
  12. |          1       | 0x8049ca0 |          2       | 0x8049ca8 |      3   | 0x8049ca4 |
  13. ______________________________________________________________________________________


  14. In main.c:
  15. ______________________________________________________________________________________
  16. | test_extern1_val | address  | test_extern2_val | address   | tmp_vale | address   |
  17. ______________________________________________________________________________________
  18. |          1       |0x8049ca0 |          2       | 0x8049ca8 |      3   | 0x8049c98 |
  19. ______________________________________________________________________________________




  20. ****************************************** Tail *****************************************

  21. [root@localhost test_extern]#
 上面注意到,tmp_vale值虽然一样,但是地址是不一样的。
  所以,在头文件中定义static变量,代码中好像使用了相同的变量,但是实际上使用的是不同的变量,在每个源文件中都有单独的变量。会造成变量多次定义,造成内存空间的浪费,而且也不是真正的全局变量。应该避免使用这种定义方式
阅读(2271) | 评论(0) | 转发(1) |
给主人留下些什么吧!~~