Possible Duplicate:
How an uninitialised variable gets a garbage value?
So when an undefined (but declared) variable is used, it contains strange value each time. How does it have such value? Is it randomly generated purposely?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
When a local variable is declared, the compiler allocates a slot within the enclosing function’s stack frame in which the variable will live. Whatever value was in that particular spot in memory before that stack frame was setup (usually from a previous function call who’s stack frame occupied that space) becomes the initial contents of that variable.
In some cases, uninitialised variables are in fact deliberately set to some value, but it’s rarely random. For instance, a debug
malloc()might set every word of a newly allocated block to 0xbadf00d, to serve as a marker that the memory hasn’t been allocated. Thus, struct members might be initialised to something other than whatever was there before. I don’t know of any compilers that do this for stack variables, but they might exist.