Take the 2-minute tour ×

Possible Duplicate:
How an uninitialised variable gets a garbage value?

How are garbage values generated in C and C++ ? Does the compiler use some random number generation technique for generating garbage values?

share|improve this question
What do you mean by "garbage values"? –  vitaut Nov 9 '10 at 18:42
what compiler is 'the compiler'? –  Sam Miller Nov 9 '10 at 18:43
It doesn't generate garbage values, it's just a byproduct of what's already in RAM. –  Joe Nov 9 '10 at 18:43
add comment

marked as duplicate by Hasturkun, dmckee, Steve Townsend, Matthew Flaschen, Loki Astari Nov 9 '10 at 19:08


3 Answers

If you mean the values of uninitialized variables, those aren't generated. They're just whatever garbage happened to be in that memory location.

int *foo = new int;
std::cout << *foo << std::endl;

New returned a pointer to some address in memory. That bit of RAM has always existed; there is no way to know what was stored there before. If it was just requested from the OS, it'll likely be 0 (the OS will erase memory blocks before giving them out for security reasons). If it was previously used by your program, who knows.

Actually, the results of using an uninitialized variable are undefined. You might get back an unpredictable number, your program may crash, or worse.

Even if you know that its safe to run the above on your platform, you shouldn't rely on that giving a random value. It will appear random, but is probably actually quite a bit more predictable and controllable than you'd like. Plus, the distribution will be nowhere near uniform.

share|improve this answer
add comment

If by "garbage values" you mean the values of uninitialized variables, it doesn't -- the value of an uninitialized variable is undefined by the standard. The garbage value you're thinking of is actually just whatever happened to be stored in that memory right before the storage for the variable was allocated from the stack or heap.

That said, some compilers offer debugging aids that will fill uninitialized variables with some well-known "magic number" to help you catch errors of this sort. For example, Microsoft Visual C++ fills uninitialized stack variables with 0xCCCCCCCC. These are specific to the compiler and usually require that you compile with debugging turned on.

share|improve this answer
add comment

The memory your program happens to use comes up in some state, controlled by how the electrons flowed at power up, funny properties of the silicion, what was in the memory before, and sometimes cosmic rays.

It isn't random, but it is extremely hard to predict.

share|improve this answer
Except when writing very low level programs (like the OS bootloader) on an embedded system, this isn't true. All memory is put into a deterministic state at boot up. And on any modern multitasking OS, its all cleared before being handed to a program. –  derobert Nov 9 '10 at 18:49
If it depends on cosmic rays, you can't really expect any computer to work very well? –  UncleBens Nov 9 '10 at 18:54
All memory is put in deterministic state at boot up? Says who? That isn't a requirement of a boot process. Certainly none of the OSes I built required it and they all seemed to work fine. Can you do it? Sure, just slows down the boot process, though. I agree that modern OSes will provide new processes with page content that is supposed to be independent of any previous content for security reasons. Zero is a choice; you could fill a page with real random values, too :-} –  Ira Baxter Nov 9 '10 at 18:56
The initial value of bits in a capacitor on power up are controlled by leakage and by external physical events. Cosmic rays affect it. Not a lot, but its plenty real. ECC prevents them from destorying data your program wrote, by enabling correction after a bit flip. No ECC? You're taking your life into your hands, especially as transistors get smaller. –  Ira Baxter Nov 9 '10 at 19:00
@Ira Baxter: On PCs for example the BIOS often does it. Its only really a requirement, I suppose, with ECC memory. –  derobert Nov 9 '10 at 19:02
show 1 more comment
