Home > other >  In c, please remember to initialize local variables
In c, please remember to initialize local variables

Time:09-22

Language, an uninitialized local variable exactly is how much?
The answer is always:
Related to the compiler,
May be but not initialized to zero,
Not sure,
In a word, is all some serious metaphysical answer, this is very annoying,
Whenever some people give you pull reel to the compiler. The C library, processor architecture gives out a practical scenario recurring problems, the probability of National People's Congress in the bull,
Actually, this question itself is wrong, that all can speak 100000 words, as long as we can in certain situations determine its specific behavior with respect to OK, of course, it's need to design an experiment comparing OK,
In the demonstration a behavior before the actual code, gives a knowledge, CPU not know variables, more can't identify the name of the variable, the CPU will only from a specific memory location value or the value to a specific memory location, so when asked what is the value of a variable, must want to know the variable corresponding value is stored in what place,
Look at the code below:
#include
Void func1 () {int a; Printf (" func1: % d \ n ", a); A=12345; }
Void func2 () {int b; Printf (" func2: % d \ n ", b); }
Void func4 () {int d; Printf (" func3 - a non-class function: % d \ n ", d); }
Void func3 - a non-class function () {int c; Printf (" func3 - a non-class function: % d \ n ", c); C=54321; Func4 (); }
Void test_call () {func3 - a non-class function (); }
Int main (int arg c, char * * argv) {func1 (); Func2 ();
Test_call (); }
We have func1 ~ func4 a total of four function, and its internal have an uninitialized local variables, how much are they?
For this kind of local variables and their values depend on:
The position of variables in the stack,
Variables corresponding to the stack position before ever been store,
As you can see, the first point marked a memory location, the second is the behavior of the code, that is to say, if we have the code to store the corresponding position, and the subsequent code does not reset the value of the position, the position will be retained is store the values of the original
Validation is very simple, to try to know:
[root @ localhost test] #./a.o utfunc1:0 func2:12345 func3 - a non-class function: 0 func3 - a non-class function: zero according to the change of the function call stack frames, func1 of local variables and func2 local variable b is obviously in the same position, in func1 is invoked, it is a new memory (which may have before entering the main stack frame reaches the position), a value depends on the page to memory the position corresponding to the initial value of the offset it depends on the operating system:
Operating system when the page is in the assigned to the program may be the page clear zero page,
Stack does not involve the distribution of the C library, there are clearly not involving the behavior of the C library, but similar malloc memory allocated is involved in the C library,
To print the results, a value of 0, we believe that the operating system is returned to the application of zero page, then will the assignment in func1 function returns after 12345, the next call func2, before func1 has withdrawn from the stack frame position to rebuild the stack frame, corresponding position still 12345,
I didn't see behind func1 ret operation stack 0 code instructions, efficiency consideration, also should not have such instructions,
Look at test_call function, obviously, func3 - a non-class function and func4 calls using is not the same stack frame, so even in func3 - a non-class function on assignment for 54321 c, will not affect on the stack frame func4 stack frame corresponding to the location of the value of d, so c and d remained to 0, the initial value of the
Then initialize a local variable and not initialize a local variable, the instruction level, where is the difference?
Eyes is very simple, just know, first see an uninitialized local variable func1:
//int a; 00000000004005 AD & lt; Func1 & gt; : 4005 AD: 55 push % RBP 4005 ae: 48 89 e5 RSP, mov % % RBP 4005 b1:48 83 ec 10 sub $0 x10, % RSP 4005 b5:8 b 45 fc mov - 0 x4 (RBP) %, % eax 4005 b8:89 c6 mov % eax and % esi 4005 ba: bf 90 07 00 mov $0 x400790 40, 4005 bf % edi: b8 00 00 00 00 mov $0 x0, % eax 4005 c4: e8 b7 fe ff ff callq 400480 & lt; Printf @ plt> 4005 c9:
C7 45 fc 30 00 00 39 x3039 movl $0, 0 x4 (RBP) % 4005 d0: c9 leaveq 4005 d1: c3 retq look at initialization of the local variable a is 2222 version://int a=2222; 00000000004005 AD & lt; Func1 & gt; : 4005 AD: 55 push % RBP 4005 ae: 48 89 e5 RSP, mov % % RBP 4005 b1:48 83 ec 10 sub $0 x10, % RSP 4005 b5: c7 45 fc 00 00 00 00 movl $0 x0, 0 x4 (RBP) % 4005 BC: 8 b 45 fc mov - 0 x4 (RBP) %, % eax 4005 bf: 89 c6 mov % eax and % esi 4005 c1: bf 90 07 00 mov $0 x400790 40, 4005 c6: % edi b8 00 00 00 00 mov $0 x0, % eax 4005 cb: e8 b0 fe ff ff callq 400480 & lt; Printf @ plt> 4005 d0: c7 45 fc 30 00 00 39 movl $0 x3039, 0 x4 (RBP) % 4005 d7: c9 leaveq 4005 d8: c3 retq just sent an instruction: 4005 b5: c7 45 fc 00 00 00 00 movl $0 x0, 0 x4 RBP (%)
Initialization operation depend on real instructions completed,
Summary, the function returns when pop out of the current stack frame is not clean it remains in the stack frame data, reuse again next function call to the stack frame memory, an uninitialized local variable will be affected by the legacy data, thus uncertain!
So, remember to initialize your local variables,