How's this possible when the program always assign a free memory location to a variable? How could it be something other than zero (I assume that default free memory value is zero)?
There is no default "free" value in memory. Memory chips just contain "static" until they are powered and something is written to them. Depending on which bits of the chip you get, they get interpreted as numbers and one might be 0. It's a coin toss what you get.
That said, many operating system kernels, for security reasons, clear memory before they hand it to your app. After all, imagine your program is run after someone used sudo; the memory you get might still contain a token that will let your program get superuser rights.
However, that only applies to the first time a bit of memory is given to your program. If you later delete it and allocate another block of same or smaller size, your malloc library might choose to just re-use that block instead of asking the kernel for more memory, and will not clear it to 0.
And that's just heap memory. On the stack, values from previous functions may still be present. But also, at initial startup, the OS (or some of the initialization code that runs before main, like constructors for global objects, or some housekeeping code your operating system runs to e.g. set up new) might have left numbers or zeroes on the stack, and since that code runs each time at startup, might do so consistently.
The stack is like an array with a maximum size. The entire stack is reserved for your thread by the kernel, and then it remembers how many items are on the stack. If you push 1, 0 and 2 on the stack, then take them off again, and then request space for 3 variables, they will still contain 1, 0 and 2. Because C doesn't clear memory (it would be a waste of time if you then immediately assign it a value afterwards), and this area is reserved for your stack, so nobody else will re-use it.
coutis from C++.coutin C?