How C + + lets static variables initialize only once

Source: Internet
Author: User

Children's shoes in the study of C + +, often just according to the text in the book to force memory of various characteristics, for example, static variables only initialized once. Your heart must be in meditation: Be sure to remember that static will only be initialized once and so, I hope I can remember. Tell you why you always remember, because you do not really understand the principle of static variables, so I would like to tell you the principle of it, directly on the code:
[Code=c/c++]
#include "stdafx.h"


int _tmain (int argc, _tchar* argv[])
{
int initnum = 3;
for (int i=5; i > 0; i.)
{
static int n1 = Initnum;
n1++;
printf ("%d\n", N1);
}


GetChar ();
return 0;
}
[/code]
Output Result:
4
5
6
7
8


Here we can see that although the code loops 5 times, the static variable N1 is really only initialized once. So why? Continue on the code, I believe you will understand a little bit.
[Code=c/c++]
int _tmain (int argc, _tchar* argv[])
{
int initnum = 3;
for (int i=5; i > 0; i.)
{
static int n1 = Initnum;


We're here for two lines of code
int* p = &n1;
p++;
*p = 0;
End


n1++;
printf ("%d\n", N1);
}


GetChar ();
return 0;
}
[/code]
Output Result:
4
4
4
4
4


This time, the static variable was initialized 5 times with 5 cycles. You must be very surprised, in fact, we are not difficult to infer, in factstatic variables are recorded by a 32-bit memory bit behind a static variable to identify whether the static variable has been initialized. and our p++;*p = 0, but each time the value is assigned to 0, so the program has always been that N1 has not been initialized, and each time initialized. Looking at the memory, it's even clearer:
0x00e8716c, XX, B0 E7 1e 6a 00 00 00, xx xx, xx xx, XX
The memory address here is the static variable n1 address, the value is 3, followed by a 1, you see, this is the program used to record whether the static variable initialization of the identity bit. Now you must understand the principle, and can easily remember the characteristics of static variables, right?
Children's shoes You can also try, multiple static variables, identify the representation of the bits in order to delve deeper (revealing that each bit identifies the initialization state of a static variable).

The above code has a point to explain: The reason for the code to use int initnum = 3, instead of directly with the static int n1 = 3, because if the static variable is directly assigned to a constant, the compiler will be optimized, so that the program at the start, the initialization of the good, It is not easy for us to observe changes in memory of static variables.

How C + + lets static variables initialize only once

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.