Go The difference between initialization and non-initialization of global variables and local variables

Source: Internet
Author: User

Original link: http://www.kingofcoders.com/viewNews.php?type=newsCpp&id=189&number=4836955386

In the C language, if the global variable is not initialized, the default is 0, that is, in the global space: int x = 0; with int x; The effect looks the same. But in fact, the difference is very large, we strongly recommend that all global variables are initialized, their main differences are as follows:

The compiler at compile time for these two situations will produce two kinds of symbols placed in the target file symbol table, for the initialization, called strong sign, uninitialized, called weak symbol. Connector when connecting to the target file, if you encounter two duplicate name symbols, there will be the following processing rules:

1, if there are more than one strong sign with the same name, then error.

2, if there is a strong symbol, a number of weak symbols, the strong symbol will prevail.

3, if there is no strong symbol, but there are multiple weak symbols with the same name, then choose a weak symbol.

In most cases, we don't want the connector to make a decision for us, so I don't agree with the latter two rules, at least to give a warning, not to pass quietly. Because the bug caused by this problem will be difficult to find, so we should try to initialize the global variables, for other files do not want to reference the variable, also try to use static decoration. Apart from the performance of the connection, the uninitialized symbol is in the BSS segment of the target file, and the initialized symbol is in the data segment.

Note: BSS segments (data not initialized manually) do not allocate space for data in that segment, just the amount of space required to record the data.
The data segment (data that has been manually initialized) allocates space for the information and the data is saved in the destination file.

For local variables that are not initialized, the values generally fall into the difference between the debug version and the release version in two cases.

Cases:

#include "stdafx.h"
int i;
int main (int argc, char* argv[])
{
printf ("i =%d\n", i);
Int J;
printf ("J=%d\n", j);
return 0;
}

In the debug version, the value of I in this code is printed out to be 0, while the value of J prints out is-858993460, that is, 0xCCCCCCCC. As for why this value, some netizens give this explanation. (designed to be 0XCCCCCCCC is a special purpose ...) This appears to be called poison, uninitialized pointer to fetch the value of the words will be wrong. Someone must have asked why not to be 0x00000000, because the null pointer is a valid state of the pointer, which can be misleading, and 0xCCCCCCCC under Windows can never be a valid state of a pointer (not NULL, not pointing to an object, not pointing to the area immediately following a heap of objects), This is the simulation of the wild hands ... )。

It is important to note that the same code in the release version, this code is not initialized in the last variable can be printed out is 0. Also has the powerful netizen to give the explanation. (The focus is on a function of the VC: Catch release-build Errors in Debug build is opened with the/GZ compile switch.) The debug version of this switch is open, release version is off (for efficiency). The switch is to initialize all dynamic local variables to 0XCCCCCCCC, initializing all dynamic heap variables to 0XCDCDCDCD. Many novices forget to initialize the variables that should be initialized (especially the new variables), and sometimes they assume that these variables should be 0, which may occur in the release version of the normal and debug version of the program is not normal, because release version at least the initial value of local variables is likely to be 0 , and sometimes they assume or expect that these variables are not 0, which brings up the most difficult bug to find.

In view of the above analysis, we recommend that you develop the habit of initializing a variable.

Go The difference between initialization and non-initialization of global variables and local variables

Related Article

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.