Reputation: 9462
I always wondered why there are garbage values stored in a memory space. Why cant the memory be filled with zeros. Is there a particular reason?
For example:
int a ;
cout<<a //garbage value displayed
Upvotes: 1
Views: 1396
Reputation: 21156
For (mostly historical) performance reasons. Zeroing out memory locations that get assigned a proper value later is unnecessary work and one of c/c++ slogans is "You don't pay for what you don't need".
Usually you should always properly initialize a variable right when it is declared anyway, but especially in c, you sometimes just don't know yet, what the initial value of a variable should be.
EDIT: If your question is about where that garbage data comes from: It is just the data that has previously been stored at the same physical address. Lets say, you are calling the following two functions directly after another:
void foo(){
int a=5;
}
void foo2() {
int b;
std::cout << b << std::endl;
}
int main() {
foo1();
foo2();
}
it is quite likely,that (in debug mode) the output of your program will be 5 (I believe, this is actually UB, so - taking into account compiler optimizations - anything can happen, of course)
Upvotes: 1
Reputation: 50190
Because it's expensive to clear memory (or certainly was), and in the vast number of common cases it wasn't needed.
In the example you show it's stack memory. It would be prohibitively expensive to zero this out each time (basically every function call would have to clear a lump of memory).
Upvotes: 1
Reputation: 48615
Assigning zeros takes time and is not always what the programmer wants to do. Consider this:
int a;
std::cin >> a;
Why waste time loading a zero into the memory when the first thing you are going to do is store a different value there?
Upvotes: 8
Reputation: 504
The garbage values come from what is present in the memory space. In your case you have only declared the variable and not initialised it. When a variable is declared, and not initialised, memory is allocated for that variable but not cleared, mostly for performance reasons. Therefore, it may contain an initial value that you do not expect it to contain, which can happen for several reasons. According to Code Complete Chapter 10, a few reasons include:
A good practice is to declare and initialise a variable as close as possible to where they're first used i.e. follow the Principle of Proximity by keeping related actions together.
Upvotes: 0
Reputation: 7006
The garbage values you are getting are the values that were previously stored in that address. But in C++ ( and many other languages ) , initializing them all to zero is quite an expensive task which the compiler does not do. That would waste a lot of time which the compiler could have used for some other purpose. So, assigning new values is not done by the compiler.
But there are other compilers that initialize them to 0, but C++ is not one of them.
Normally, the compiler will expect you to give the new variables a new value. Like
int a = 0; // this is not so hard to do
or
int a;
std::cin >> a ;
So, us assigning a value is much more efficient than the compiler initializing it and then overwriting it.
if you don't assign them values before accessing them, the compiler will give you a warning about uninitialized variable. ( if you have compiler warnings turned on ).
Upvotes: 0
Reputation: 18964
Modern OSs do initialise memory to 0 before your process first gets access to it. But once it's been used once there's generally no point zeroing it out again unless there's a specific need. The "garbage values" are just whatever was last written to that memory.
Upvotes: 1