2

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?

Community
  • 1
  • 1
krackoder
  • 2,841
  • 7
  • 42
  • 51

3 Answers3

9

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.

derobert
  • 49,731
  • 15
  • 94
  • 124
3

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.

Nick Meyer
  • 39,212
  • 14
  • 67
  • 75
0

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.

Ira Baxter
  • 93,541
  • 22
  • 172
  • 341
  • 1
    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 09 '10 at 18:49
  • If it depends on cosmic rays, you can't really expect any computer to work very well? – UncleBens Nov 09 '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 09 '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 09 '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 09 '10 at 19:02
  • Its not even a requirement on ECC memory. When the processor *writes* a value to memory the first time, the ECC gets set properly. Doesn't matter what state the ECC is for a location if it isn't *read*. All the program has to do is to not look at values it hasn't initialized. There have actually been proposals to set the ECC wrong on all memory locations initially to help find uninitialized variables :-} – Ira Baxter Nov 09 '10 at 20:04