C/C++ 静态变量解析

静态变量:这种变量的内存将保存到整个程序的结束,他的内存是独立存放到一个叫做静态内存区的。
          所有的静态的变量如果不赋值,会默认赋值为0,不管是结构体还是其他类型的变量, 当然静态变量的初始化也分为静态初始化和动态初始化,如果有一些不可确定的因素会使用动态初始化
          比如:
          int a = 1;静态初始化。
          double a = sqrt(2);动态初始化因为sqrt是一个不确定的因素。
          有几个概念
          内部全局:内部是说不能被其他文件调用,全局是说有限访问范围为本文件。
          外部全局:外部是说可以被其他文件调用,全局是说有限访问范围为本文件。
          内部局部:内部是说不能被其他文件调用,局部是说有限访问范围为本代码块。
举例3种模式
1、内部局部静态变量,如
int test(void)
{
static i = 1;
.....
}
这个静态变量是局部的,有效范围是本代码块。
2、外部全局静态变量
int i = 1;
int test(void)
{
   ....
}
这个静态变量是全局的,有效范围是本文件。
并且它是外部的,所以其他文件如果想使用可以使用
extern int i;
3、内部全局静态变量
static i = 1;
int test(void)
{
   ....
}
这个静态变量是全局的,有效范围是本文件。
并且它是内部的,不能被其他文件调用,如果想extern int i;是不行的。
注意extern int i;不能初始化,因为他只是声明而已,实际的变量在其他文件进行了空间初始化,也就是说内存已经分配了这里extern int i;不会分配任何内存空间。
这里展开说一下关于左值和右值。
任何一个有限的内存空间才能作为左值,

int a=1;
int *p;
p = &a;
*p = 1;
是可以作为左值的,如果没有有效内存一定不行
比如

int a;
int b;
a+b = 5;
这里的a+b 是一段临时空间不能作为左值

但是关于如果我们抓住一个细节就是这段静态内存区域在整个程序期间都存在,C/C++是很灵活的,我们就可以定义一个指针来访问他。
我写了一段程序。这段程序包含了其他的一些东西,比如flexible array number 来节约内存,

  1. t1.h
  2. #include<iostream>
  3. using namespace std;
  4. #ifndef TEST_H__
  5. #define TEST_H__
  6. #include <stdlib.h>
  7. #include <string.h>
  8. #include <stdio.h>
  9. typedef struct chain
  10. {
  11.          char address[];
  12. } CM;
  13. typedef struct mc
  14. {
  15.         int id;
  16.         char name[20];
  17.         CM* adr;
  18. } CHC;
  19. #define LEN_S strlen(max_address)+sizeof(CM)+3
  20. #define LEN_MAX strlen(max_address)
  21. CM* finps(CHC* ins);
  22. void fshows(const CHC* ins);
  23. int** stre(void);
  24. #endif
  25. t3.cpp
  26. #include<iostream>
  27. #include "t1.h"
  28. using namespace std;
  29. static int st1 = 1;
  30. static int st2 = 2;
  31. int** stre(void)
  32. {
  33.         static int* p[2];
  34.         p[0] = &st1;
  35.         p[1] = &st2;
  36.         return p;
  37. }
  38. CM* finps(CHC* ins)
  39. {
  40.         char max_address[80];
  41.         static CM* ck;
  42.         cout<< "please input id:" <<endl;
  43.         cin >> ins->id;
  44.         cin.get();
  45.         cout<<"please input name:"<<endl;
  46.         cin.getline(ins->name,19);
  47.         cout<<"please input your address"<<endl;
  48.         cin.getline(max_address,79);
  49.         ck = (CM* )malloc(LEN_S);
  50.         ins->adr = ck;
  51.         strcpy(ck->address,max_address);
  52.         return ck;
  53. }
  54. void fshows(const CHC* ins)
  55. {
  56.         cout<<"your information is:"<<endl;
  57.         cout<< ins->id <<endl;
  58.         cout<< ins->name <<endl;
  59.         cout<< ins->adr->address <<endl;
  60. }
  61. t2.cpp
  62. #include<iostream>
  63. #include "t1.h"
  64. using namespace std;
  65. int main(void)
  66. {
  67.         CHC ins;
  68.         CM* dt;
  69.         int** p;
  70.         dt = finps(&ins);
  71.         fshows(&ins);
  72.         free(dt);
  73.         p = stre();
  74.         cout<<"access static values from pointer:"<<endl;
  75.         cout<<**p<<endl;
  76.         cout<<**(p+1)<<endl;
  77. }

注意这里的

  1. static int st1 = 1;
  2. static int st2 = 2;
  3. int** stre(void)
  4. {
  5.         static int* p[2];
  6.         p[0] = &st1;
  7.         p[1] = &st2;
  8.         return p;
  9. }



实际上我定义了一个指针数组来保存st1和st2的地址,并且返回了指向指针的指针p
那么我们访问的时候就可以
<endl;

  1. cout<<**p<<endl;
  2. cout<<**(p+1)<<endl;

<endl;
来进行访问了,这样一来虽然定义的内部全局静态变量但是任然访问到了。


编译执行这段程序:
gaopeng@bogon:~/CPLUSPLUS/part9$ g++  t2.cpp t3.cpp
gaopeng@bogon:~/CPLUSPLUS/part9$ ./a.out 
please input id:
1
please input name:
gaopeng
please input your address
chongqing
your information is:
1
gaopeng
chongqing
access static values from pointer:
1
2
< /endl;
< /endl;


--end---

猜你喜欢

转载自www.linuxidc.com/Linux/2016-06/132672.htm