What does it mean to use static to define a variable in a header file

Source: Internet
Author: User

Article Source: http://www.cnblogs.com/zplutor/

See a classmate in the header file in this write:

1 Static Const wchar_t* g_str1 = ... 2 Static Const wchar_t* g_str2 = ...

This way of defining variables I've never seen, and it's going to go through compiling, so I'd like to know how the compiler handles this variable definition.

Using static when defining a global variable means that the scope of the variable is limited to the source file that defines it, and other source files cannot be accessed. Since this definition appears in the header file, it is natural to speculate that the variables are defined in all source files that contain the header file, that is, how many times the header file is included, and how many times the variables are defined.

If you remove the static of the above two lines of code, the compiler will have a variable redefinition error, which further confirms the above speculation, because there is no static, the scope of the variable is global, the definition of more than two variables of the same name will appear this error.

Speculation, after all, is to prove that this speculation has to be verified by writing code. This is verified by using the static definition variable in the header file, including the header file in multiple source files, and then outputting the address of the variable in each source file, changing the value of the variable in one source file, and outputting it in another source file. If the output of each source file is different, it is inferred, otherwise the inference is wrong.

The following is the code that defines the header file for the variable:

1 // Header.h 2 #pragma once34staticint3;

Next, declare two test functions in a different header file:

1 // Functions.h 2 #pragma once34void  TestSource1 (); 5 void TestSource2 ();

Define these two test functions in each of the two source files:

1 //Source1.cpp2#include <stdio.h>3#include"Header.h"4 5 voidTestSource1 () {6 7wprintf (L"G_int ' s address in Source1.cpp:%08x\n", &g_int);8G_int =5;9wprintf (L"G_int ' s value in Source1.cpp:%d\n", g_int);Ten}
1 //Source2.cpp2#include <stdio.h>3#include"Header.h"4 5 voidTestSource2 () {6 7wprintf (L"G_int ' s address in Source2.cpp:%08x\n", &g_int);8wprintf (L"G_int ' s value in Source2.cpp:%d\n", g_int);9}

Finally, the two test functions are called in the main function:

1 // Main.cpp 2 " Functions.h " 3 4 int wmain () {56    TestSource1 (); 7     TestSource2 (); 8 }

Run the program:

As you can see, although it seems that the same variables are used in your code, you are actually using different variables, each with a separate variable in each source file. Therefore, defining a static variable in a header file causes the variable to be defined more than once, creating a waste of memory space and not a real global variable. You should avoid using this definition method.

As a comparison, the following is the correct way to define global variables:

1 // Header.h 2 #pragma once34externint g_int;
1 //Source1.cpp2#include <stdio.h>3#include"Header.h"4 5 intG_int =3;6 7 voidTestSource1 () {8 9wprintf (L"G_int ' s address in Source1.cpp:%08x\n", &g_int);TenG_int =5; Onewprintf (L"G_int ' s value in Source1.cpp:%d\n", g_int); A  -}

The other files do not change.

To run the program:

As you can see, the same variable is used in all two source files. It is important to note that using extern to declare a variable cannot have an initial value, otherwise it is still a variable definition and there is an error in variable redefinition.

What does it mean to use static to define a variable in a header file

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.