Search code examples
cvariablescompiler-constructionprogramming-languagesinitialization

How does an uninitiliazed variable get a garbage value?


When we create a variable and don't initialize it, then some (random) number called garbage value is assigned to it.

  • How this value is assigned to the variable?
  • What is whole concept/mechanism behind this?
  • Does this happen only in C?

Solution

  • The garbage value is not assigned, rather the value is already there. When you allocate a variable you are reserving a piece of memory - until you overwrite it that memory will contain whatever "random" information was there before.

    As a metaphor, think of allocating a variable like buying a piece of land - until you do something with it (like build a house) the land will just have whatever trash was already sitting there (like an old crumbling house).

    Some languages will automatically fill newly allocated variables with zeros - this takes time to do. In more "do-it-yourself" languages like C this extra behavoir is not guarenteed (though on some systems memory is cleared regardless of language, for example as a security measure)