问题
I'm playing with c++ in VisualStudio2010
Please explain why IT happens:
int a, b;
int *p, *q;
cout << a << " " << b;
prints out "0 0". Well it's understandable, uninitialized integer should be 0; but
int a, b;
int *p, *q;
p = &a;
cout << a << " " << b;
output is "1792816880 0"
So if I assign pointer to uninitialized variable it change value from default. Why?
Edit clarification: the question was not about value of uninitialized variable
int a; int *p;
cout << a; // would be 0, because it's loacal variable
p = &a;
cout << a; //not 0;
How getting pointer of a could change it value? when we init variable, we allocate space, some bits, they could be anything, but "p = &a" does it actually change bits in this space?
回答1:
Well it's understandable, uninitialized integer should be 0
No.
There is no guarantee of that, it depends on the storage class, If your int is local variable it has a auto storage and it need not be 0.
Accessing Unitialized variables causes Undefined Behavior and Your code is causing an Undefined Behavior. Once there is an Undefined Behavior all bets are off and the behavior cannot be explained.
Regarding Undefined Behavior,
C++ Standard section 1.3.24 states:
Permissible undefined behavior ranges from ignoring the situation completely with unpredictable results, to behaving during translation or program execution in a documented manner characteristic of the environment (with or without the issuance of a diagnostic message), to terminating a translation or execution (with the issuance of a diagnostic message).
EDIT:
Given the above that this is Undefined Behavior and One should not write any code which relies on such behaviors, infact One should not even think of writing such code. I find it irrelevant & unproductive to dig in to the implementations of all those compilers to seek an explanation of why it works this way.
If someone finds this reply hardheaded, You are free to downvote. If the downvote count exceeds the upvotes, I would know the answer is unwelcome and I will delete it.
回答2:
This is "undefined behavior" in C++.
When you do not initialize a variable in C++ it is not always zero. That is just by chance. The memory is allocated, and the garbage value which was in that memory location previously will be outputted until it is initialized.
回答3:
The value of an uninitialized value is not defined. There is no default value. Any value could show up.
Any seemingly unrelated operation you do could change the underlying value of the garbage which the software will give you as an answer to your query of an undefined value.
The bottom line is, do not speculate about what undefined behaviour should happen. It is simply undefined - do not write code with undefined behaviour.
回答4:
It's undefined behaviour but here is some speculation as to what might be happening:
In the first code sample, the address of the variables is never taken so possibly the compiler has chosen to keep them in registers and they never exist in main memory, and the value of the register happens to be 0.
In the second code sample, the address is taken so the variable has to be in memory (on the stack) so that it can have an address. It just happens that the existing data there was some random garbage.
回答5:
You've got a bunch of variables that are not written to, ever. A compiler may decide not to allocate storage for such variables. This may seem harsh, but it's reasonable: modern compilers allocate variable storage on a fine-grained level, and some variables may be unused on some branches. Why bother allocating memory when it's not needed ?
Now, what happens if you read from a "variable" that wasn't allocated memory because you never wrote to it? You'll get some random data, quite possibly another variable. Or a segfault - Undefined Behavior comes in many forms.
Now take your second example: here you do write to one variable, p. Since a didn't need any memory allocation (lacking a write), it may very well alias p. And the value of p, reinterpreted as an int could very well be 1792816880.
回答6:
C++ does not initializes variables on declaration it just allocate RAM memory for them. They can by any value before initialize no metter if you use pointer or not. You should always initialize your variables before use.
回答7:
Uninitialized local integers are not guaranteed to be 0 in C++. They are "garbage", hence they can be any value including 0.
来源:https://stackoverflow.com/questions/7468151/assigning-pointer-to-uninitialized-variable-changes-it-value