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

Source: Internet
Author: User

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, it is strongly recommended that all the global variables
are initialized, their main differences are as follows:

At compile time, the compiler will produce two kinds of symbols in the target file's symbol table for both cases, for the initialization, called strong
Symbols, uninitialized, are called weak symbols. Connector when connecting to the target file, if you encounter two duplicate name symbols, there will be the following processing rules
The
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 give a warning,
And should not be passed quietly. Because the bug caused by this problem will be difficult to check, so we should try to initialize the global variables, for not want to give
Variables referenced by other files, as well as static modifiers.

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: A BSS segment (data that is not manually initialized) does not allocate space to the data for that segment, but only the amount of space required to record the data. The
data segment, which has been manually initialized, allocates space for the data and the data is saved in the destination file.
for local variables, not initialized, the value is generally divided into two cases of debug version and release version of the difference.
Example:
#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 to why this is the value, some netizens give this explanation. (designed to be 0XCCCCCCCC is a special purpose ...) This appears to be called
Poison, and an uninitialized pointer will go wrong when the value is taken. Someone must have asked why not to be 0x00000000, because a null pointer is a valid state of a
pin, which can be misleading, and 0xCCCCCCCC under Windows can never be a valid state of a pointer (not NULL,
does not point to an object, Does not point to a bunch of objects immediately following the area), this is the simulation of the wild pointer ...
It is worth noting that the same code in release version, the uninitialized variables in this code will be the last to print out a 0. The
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 and initialize all dynamic heap variables to 0XCDCDCDCD. Many novices forget to initialize these
variables that should have been initialized (especially new variables), and sometimes they assume that these variables should be 0, which may appear in the
release version of the normal and debug version of the program is not normal, Because the initial value of at least local variables in release is likely to be 0, and sometimes they
assume or expect that these variables are not 0, which brings up a bug that is hardest to find)

From:http://www.kingofcoders.com/viewnews.php?type=newscpp&id=189&number=4836955386

--------------------------------------------------------------------------------------------------------------- ----------------------

However, the individual to verify the local variables, uninitialized, access will be compiled directly.

The value of a variable that is not initialized by a member object in a class differs in the way that the object is constructed:
struct NPC
{
int m;
int data;
};
npc* PNPC = new Npc; New NPC ()
printf ("m Default value =%d\n", pnpc->m);
NPC () invokes the default constructor, which initializes the member variable to the 0,new NPC, but the value of data is indeterminate.

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

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.