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?
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?
It is not randomly generated, it is just residual memory.
It would be highly inefficient to clear all unused memory every time. So the memory is released to the OS and made available. When you request new memory, you get some of this memory, which is not owned by anyone but still has garbage in it since it was just freed, but not cleared.
C is not cleaning the memory it allocates. These values are the 'leftover' in the allocated location in memory.
C as a principle is not hiding operations from the programmer. It does only what asked to.Since you did not ask to initialize the variable, it doesn't do it for you, so the same bits that were 'up'/'down' in the destination memory - do not change.
Variable can have undefined values to avoid overhead of initializing with some sensible values, so it is definitely not generated randomly (which is itself nontrivial operation). The value variable initially holds is just what happens to be in variable's memory location at that time.
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.
The value of a unitialized value in c depends of whatever the value stored at the memory address was. It isn't randomized on purpose.
Whenever you declare a variable, it will already have a memory space to hold it's value. If you don't set anything explicitly, it will contain whatever value that was previously stored on that location. So it's not randomly generated purposely by the program, just the value that happens to be there.
Declaration of a variable is a indication to the compiler saying that there is one such variable of so and so type. Definition of a variable allocates memory for that. The allocated memory could be anywhere from stack
(auto
variables), heap
(dynamically allocated memory), etc. Unless it is a static
variable, it will be allocated memory from an un-initialized data segment. So the random values that you are seeing is nothing but the values that are stored in that memory locations earlier! So it is advised to initialize the variables before using them (for the first time) or in other words, do not use/de-reference un-initialized variables/pointers.
More information on a structure of a program in memory can be found here.
Hope it helps!