如何在uninitiliazed变量得到一个垃圾的价值? [英] 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.
- 此值如何分配给变量?
- 什么是整体的概念/这背后的机制呢?
- 这是否只发生在C 3
推荐答案
垃圾值不是的分配的,而该值已经存在。当你分配你保留的一块内存变量 - 直到你覆盖它内存将包含一切随机的信息在那里之前
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).
有些语言会自动用零填充新分配的变量 - 这需要时间做。在更多的做自己动手的语言,如C这种额外behavoir没有保证的广告(尽管在某些系统上的内存,无论是语言的清除,例如,作为一种安全措施)
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)
这篇关于如何在uninitiliazed变量得到一个垃圾的价值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!