[C/C++] What does it mean to use static to define variables in header files

Can global variables be defined in header files that can be included by multiple .C files?

Answer: Yes, you can declare a global variable with the same name in the form of static in different C texts, and you can declare a global variable with the same name in the C file of the Muppets, provided that only one C file can assign this variable to the variable. The initial value, the connection will not go wrong at this time!

It is recommended not to define variables in header files, only variable declarations are made in header files!

The reasons why it should be declared static are as follows:

1" Each .c file will be compiled into an obj, so that if you define it in the header file instead of declaring it, there will be an instance of this variable in each obj that references this header file, and it will be used when connecting. A duplicate definition error will be reported.

2" The scope of static variables is only valid within the compilation unit


I saw a classmate wrote this in the header file:

1staticconst wchar_t* g_str1 =2staticconst wchar_t* g_str2 = …

I've never seen variables defined this way and it compiles without a hitch, so I'm curious to see how the compiler handles variable definitions like this.

Using static when defining a global variable means that the scope of the variable is limited to the source file in which it is defined, and other source files cannot access it. Since this definition method appears in the header file, it is natural to speculate that these variables are defined in all source files that include the header file, that is, how many times the header file is included, the number of these variables are defined. Second-rate.

If the static of the above two lines of code is removed, a variable redefinition error will occur during compilation, which further confirms the above speculation, because the scope of the variable is global without static, and two or more variables with the same name are defined This error occurs.

推测终究是推测,要真正证实这个推测还要通过写代码来验证。验证的方式是:在头文件中使用static定义变量,在多个源文件中包含该头文件,然后在每个源文件中输出变量的地址,同时在一个源文件中改变变量的值并输出,在另一个源文件中也输出。如果每个源文件的输出都不同,则推测得证;否则推测是错误的。

下面是定义变量的头文件的代码:

1//Header.h2#pragma once
34staticint g_int = 3;

接下来在另一个头文件中声明两个测试函数:

1//Functions.h2#pragma once
34void TestSource1();
5void TestSource2();

分别在两个源文件中定义这两个测试函数:

 1//Source1.cpp 2 #include  3 #include "Header.h" 4 5void TestSource1() {
 6 7     wprintf(L"g_int's address in Source1.cpp: %08x\n", &g_int);
 8     g_int = 5;
 9     wprintf(L"g_int's value in Source1.cpp: %d\n", g_int);
10 }
1//Source2.cpp2 #include 3 #include "Header.h"45void TestSource2() {
67     wprintf(L"g_int's address in Source2.cpp: %08x\n", &g_int);
8     wprintf(L"g_int's value in Source2.cpp: %d\n", g_int);
9 }

最后在main函数中调用这两个测试函数:

1//Main.cpp2 #include "Functions.h"34int wmain() {
56    TestSource1();
7    TestSource2();
8 }

运行该程序:

 可以看到,虽然在代码中好像使用了相同的变量,但是实际上使用的是不同的变量,在每个源文件中都有单独的变量。所以,在头文件中定义static变量会造成变量多次定义,造成内存空间的浪费,而且也不是真正的全局变量。应该避免使用这种定义方式。

作为对比,下面使用正确的方式来定义全局变量:

1//Header.h2#pragma once
34externint g_int;
 1//Source1.cpp 2 #include  3 #include "Header.h" 4 5int g_int = 3;
 6 7void TestSource1() {
 8 9     wprintf(L"g_int's address in Source1.cpp: %08x\n", &g_int);
10     g_int = 5;
11     wprintf(L"g_int's value in Source1.cpp: %d\n", g_int);
1213 }

其它文件不变。

运行程序:

 可以看到,这次两个源文件中使用的都是同一个变量。要注意的是,使用extern声明变量时不能带有初始值,否则仍然属于变量定义,会出现变量重定义的错误。

参考:点击打开链接

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325979671&siteId=291194637