5

Whenever a variable dies, the memory is erased. But is the content of the memory erased? And if so what happens to the contents of the memory ? Why do we see garbage values (that many times does not make any sense)?

4

accessing a variable after it went out of scope is not defined in C. I assume by "erased" you mean "zero-filled" or something and, in this case, no, the memory is not zero-filled because why bother?

what happens is that the memory that this variable used to identify is again available to use after this variable goes out of scope and it may be reused when functions are called, other variables are created, etc.

this is exactly why you see garbage values. that is, when a local variables is created, it may identify a previously-used piece of memory that happened to have this value.

| improve this answer | |
  • Right Kareem. I think that way but just needed a confirmation. – Gautam Sharma Jul 4 '15 at 2:48
2

What happens to memory varies from one programming language to another, and sometimes from one platform to another. With some languages, a variable is discarded from the program, such as when a variable goes out of scope. A good example is when you call a function in C. Vars are created inside the function and data is stored there, but later, you leave the function and the vars are discarded. In reality, the memory that was allocated to the function (in this case, on the stack - see later lessons) is locked for use by the function when the car is created, and then is released when it goes out of scope such as when exiting the function. Once released, it can be reused elsewhere. In reality, and usually, nothing is done to clear that memory because it doesn't matter. If/when it is allocated to something else later, it will be reloaded by whatever program uses it. It's just not efficient to do anything with it once it's deallocated.

Think of it this way. You have a pad of paper with a bunch of stuff written on it that no longer has any value. (We're ignoring any security concerns here.) You would simply throw it away. Does it serve any purpose to erase all the notes before you throw it away? Of course not. Same thing - the memory is the pad of paper and the data is the notes!

Note that, depending on the language, and the variable type, when a variable is declared, it may or may not be automatically initialized. For instance, some languages will automatically allocate 0 to any int or float upon creation unless it is also assigned a different value at that time. On the other hand, pointers are often never initialized by default.

Now, let's say you allocate memory with a malloc command. That bit of memory is somewehre else in the computer - the heap. (again, later lessons.) The malloc allocates the memory space you requested by locking it for your program's use and assigns it to the variable name/address that you used in the malloc command, but it doesn't initialize anything. If the computer was recently restarted, there's a good chance that it will contain all zeros. On the other hand, if it's been running for a long time, there's a good chance that there's residual data there from previous memory allocations and releases as previously described. Because of this, you should assume that the newly allocated memory may contain anything, so treat it as garbage.

Once you release the memory, say with the free command, the memory gets released back to the pool of available memory for any program to use. It will still contain the data that was there, but is no longer accessable as something useful.

Some languages, like Java, also do something called 'garbage collection'. The system will go around and reclaim available memory and do other housekeeping tasks. But that's another lecture. ;-)

For more info, you should google memory allocation for a given language.

If this answers your question, please accept this answer to remove the question from the unanswered question pool. Let's keep up on forum housekeeping. ;-)

| improve this answer | |
-2

Garbage values generally are not meaningless. It depends on compiler to compiler. Suppose you print the value of a variable "a" whose value is nothing. In that case compiler checks the address where variable is created in RAM, it's scope, etc. And gives the output as some alphanumerical/numerical value which has generally no sense but that's speaks about nature of variable. Sometimes it shows any arbitary previous value as a garbage value too in current variable.

| improve this answer | |

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .