能否在头文件中定义全局static变量?

简介:

能否在头文件中定义全局static变量?

一、在没有类定义的头文件中定义全局static变量g_static。

用gcc和g++都可以编译。但运行结果显示,在test.c和main.c中,变量的值相同,但地址不同,说明是两个变量。

frank@userver:~/project/test/static-test/static1_c$ cat static.h 

#ifndef STATIC_H

#define STATIC_H

static int g_static = 56;

#endif // #ifndef STATIC_H

frank@userver:~/project/test/static-test/static1_c$ cat test.h

#ifndef TEST_H

#define TEST_H

void test();

#endif // #ifndef TEST_H

frank@userver:~/project/test/static-test/static1_c$ cat test.c 

#include <stdio.h>

#include "static.h"

#include "test.h"

void test(){

    printf("[%s] g_static(%p,%u)\n", __FILE__, &g_static, g_static);

}

frank@userver:~/project/test/static-test/static1_c$ cat main.c 

#include <stdio.h>

#include "static.h"

#include "test.h"

int main(){

    printf("[%s] g_static(%p,%u)\n", __FILE__, &g_static, g_static);

    test();

        return 0;

}

frank@userver:~/project/test/static-test/static1_c$ g++ main.c test.c 

frank@userver:~/project/test/static-test/static1_c$ ./a.out 

[main.c] g_static(0x601040,56)

[test.c] g_static(0x601044,56)

frank@userver:~/project/test/static-test/static1_c$ gcc main.c test.c 

frank@userver:~/project/test/static-test/static1_c$ ./a.out 

[main.c] g_static(0x601040,56)

[test.c] g_static(0x601044,56)

二、在有类定义的头文件中定义全局static变量。

用g++可以编译。

在类的成员函数中,g_static的地址保持一致,说明在类的不同的对象中会访问到同一个全局static变量。

在类外部的函数中,运行结果与编译顺序有关。

在先编译的文件的函数中,g_static地址与类的成员函数一致,说明与类内部的g_static是同一个变量。。

但是在后编译的文件的函数中,g_static的地址不同,说明是另一个变量。


frank@userver:~/project/test/static-test/static1_cpp$ cat static.h 

#ifndef STATIC_H

#define STATIC_H


#include<iostream>

using std::cout;

using std::endl;


static int g_static = 10;

class TestType{

public:

    TestType(){}

    void Show()const{

        cout << "this=" << this << ", ";

        cout << "g_static:(" << &g_static << ", " << g_static << ")\n";

    }

};


#endif // #ifndef STATIC_H

frank@userver:~/project/test/static-test/static1_cpp$ cat test.h

#ifndef TEST_H

#define TEST_H

void test();

#endif // #ifndef TEST_H


frank@userver:~/project/test/static-test/static1_cpp$ cat test.cpp 

#include "test.h"

#include "static.h"


void test(){

    cout << __func__ << endl;

    cout << "g_static = (" << &g_static << ", " << g_static << ")\n";

    TestType obj;

    obj.Show();

}

frank@userver:~/project/test/static-test/static1_cpp$ cat main.cpp 

#include "test.h"

#include "static.h"


int main(){

    cout << __func__ << endl;

    cout << "g_static: (" << &g_static << ", " << g_static << ")\n";

    TestType obj;

    obj.Show();

    test();

}

frank@userver:~/project/test/static-test/static1_cpp$ g++ main.cpp test.cpp 

frank@userver:~/project/test/static-test/static1_cpp$ ./a.out 

main

g_static: (0x602078, 10)

this=0x7fff09eba08f, g_static:(0x602078, 10)

test

g_static = (0x60207c, 10)

this=0x7fff09eba05f, g_static:(0x602078, 10)

frank@userver:~/project/test/static-test/static1_cpp$ g++ test.cpp main.cpp 

frank@userver:~/project/test/static-test/static1_cpp$ ./a.out 

main

g_static: (0x60207c, 10)

this=0x7fffdad480ef, g_static:(0x602078, 10)

test

g_static = (0x602078, 10)

this=0x7fffdad480bf, g_static:(0x602078, 10)

frank@userver:~/project/test/static-test/static1_cpp$ 

三、结论:不要在头文件中定义全局static变量。


      本文转自FrankNie0101 51CTO博客,原文链接:http://blog.51cto.com/frankniefaquan/1936686,如需转载请自行联系原作者








相关文章
|
2月前
|
编译器 C++
c++关于命名空间内变量和函数及全局变量的使用和作用域
c++关于命名空间内变量和函数及全局变量的使用和作用域
39 1
|
2月前
|
编译器 程序员 数据安全/隐私保护
C++类成员解析:编译器如何识别和处理声明与定义(C++ 类的作用域以及查找顺序)
C++类成员解析:编译器如何识别和处理声明与定义(C++ 类的作用域以及查找顺序)
14 0
|
7月前
|
编译器
引用头文件的操作
引用头文件的操作。
26 0
|
4月前
|
Shell
变量的定义和引用
变量的定义和引用。
32 0
|
5月前
|
存储 编译器 程序员
【新手解答2】深入探索 C 语言:变量名、变量 + 函数声明 vs 函数定义 + main的声明 + 头文件和源文件的关系
【新手解答2】深入探索 C 语言:变量名、变量 + 函数声明 vs 函数定义 + main的声明 + 头文件和源文件的关系
59 0
|
安全 Unix vr&ar
一文刨析C/C++全局常量的定义
一文刨析C/C++全局常量的定义
|
C++
VS中,一个头文件使用另外一个头文件的静态变量,要谨慎
VS中,一个头文件使用另外一个头文件的静态变量,要谨慎
50 0
全局变量的声明
全局变量的声明
130 0
|
小程序
为小程序自定义全局方法和全局变量
原生小程序项目开发中,有这个情景,需要将某个方法或者变量,定义到全局变量,来方便全局使用
361 0
|
存储 程序员 编译器
容易混淆的基本概念 成员变量 局部变量 全局变量
在实际开发与学习中,特别容易混淆几个基本概念:成员变量、局部变量、全局变量。了解这些概念的属性,存储在实际编码中非常有用。
113 0
容易混淆的基本概念 成员变量 局部变量 全局变量