0

Does the allocated memory holds the garbage value since the start of the OS session? Does it have some significance before we name it as a garbage value in our program runtime session? If so then why?

I need some advice on study materials regarding linux kernel programming, device driver programming and also want to develop an understanding on how the computer devices actually work. I get stuck into the situations like the "garbage value" and feel like I have to study something else also for better understanding of the programming language. I am studying by myself and getting a lot of confusing situations. Any advice will be really helpful.

Gilles 'SO- stop being evil'
  • 104,111
  • 38
  • 209
  • 254
Anshu
  • 3
  • 1
  • 3
    I doubt that this has to do with garbage-collection. – Jonas Schäfer Aug 24 '12 at 09:53
  • any sources you could refer us to regarding to what the term "garbage value" in your question refers to? because usually, this would simply refer to "some random value which happens to be in memory" (e.g. because we haven't initialized that place yet) – codeling Aug 24 '12 at 09:56
  • The C language, when the variable is only defined and printed out. I just needed the info on why this happens. – Anshu Aug 24 '12 at 09:58

5 Answers5

4

"Garbage value" is a slang term, meaning "I don't know what value is there, or why, and for that reason I will not use the value". It is "garbage" in the sense of "useless nonsense", and sometimes it is also "garbage" in the sense of "somebody else's leavings".

Formally, uninitialized memory in C takes "indeterminate values". This might be some special value written there by the C implementation, or it might be something "left over" by an earlier user of the same memory. So for examples:

  • A debug version of the C runtime might fill newly-allocated memory with an eye-catcher value, so that if you see it in the debugger when you were expecting your own stored data, you can reasonably conclude that either you forgot to initialize it or you're looking in the wrong place.
  • The kernel of a "proper" operating system will overwrite memory when it is first assigned to a process, to avoid one process seeing data that "belongs" to another process and that for security reasons should not leak across process boundaries. Typically it will overwrite it with some known value, like 0.
  • If you malloc memory, write something in it, then free it and malloc some more memory, you might get the same memory again with its previous contents largely intact. But formally your newly-allocated buffer is still "uninitialized" even though it happens to have the same contents as when you freed it, because formally it's a brand new array of characters that just so happens to have the same address as the old one.

One reason not to use an "indeterminate value" in C is that the standard permits it to be a "trap representation". Some machines notice when you load certain impossible values of certain types into a register, and you'd get a hardware fault. So if the memory was previously used for, say, an int, but then that value is read as a float, who is to say whether the left-over bit pattern represents a so-called "signalling NaN", that would halt the program? The same could happen if you read a value as a pointer and it's mis-aligned for the type. Even integer types are permitted to have "parity bits", meaning that reading garbage values as int could have undefined behavior. In practice, I don't think any implementation actually does have trap representations of int, and I doubt that any will check for mis-aligned pointers if you just read the pointer value -- although they might if you dereference it. But C programmers are nothing if not cautious.

Steve Jessop
  • 273,490
  • 39
  • 460
  • 699
2

What is garbage value?

When you encounter values at a memory location and cannot conclusively say what these values should be then those values are garbage value for you. i.e: The value is Indeterminate.
Most commonly, when you use a variable and do not initialize it, the variable has an Indeterminate value and is said to possess a garbage value. Note that using an Uninitialized variable leads to an Undefined Behavior, which means the program is not a valid C/C++ program and it may show(literally) any behavior.

Why the particular value exists at that location?

Most of the Operating systems of today use the concept of virtual memory. The memory address a user program sees is an virtual memory address and not the physical address. Implementations of virtual memory divide a virtual address space into pages, blocks of contiguous virtual memory addresses. Once done with usage these pages are usually at least 4 kilobytes. These pages are not explicitly wiped of their contents they are only marked as free for reuse and hence they still contain the old contents if not properly initialized.

Alok Save
  • 202,538
  • 53
  • 430
  • 533
0

On a typical OS, your userspace application only sees a range of virtual memory. It is up to the kernel to map this virtual memory to actual, physical memory.

When a process requests a piece of (virtual) memory, it will initially hold whatever is left in it -- it may be a reused piece of memory that another part of the process was using earlier, or it may be memory that a completely different process had been using... or it may never have been touched at all and be in whatever state it was when you powered on the machine.

Usually nobody goes and wipes a memory page with zeros (or any other equally arbitrary value) on your behalf, because there'd be no point. It's entirely up to your application to use the memory in whatever way you please, and if you're going to write to it anyway, then you don't care what was in it before.

Consequently, in C it is simply not allowed to read a variable before you have written to it, under pain of undefined behaviour.

Kerrek SB
  • 464,522
  • 92
  • 875
  • 1,084
  • "Usually nobody goes and wipes a memory page with zeros" => not always true http://stackoverflow.com/questions/6004816/kernel-zeroes-memory – codeling Aug 24 '12 at 10:00
  • @nyarlathotep: Thanks, very interesting! It's indeed a tradeoff between "don't-pay-for-what-you-don't-use" efficiency and security concerns at a system level. – Kerrek SB Aug 24 '12 at 10:03
  • Usually it's not a big efficiency cost anyway, given that it only needs to happen when pages are mapped, which itself is kind of slow. If the clearing were considered costly then the OS could work on unallocated pages as a low-priority background task when the machine was otherwise idle, although I don't know whether Linux actually does or not. For some kinds of memory (flash), clearing a contiguous range of blocks is very cheap. – Steve Jessop Aug 24 '12 at 10:35
0

If you declare a variable without initialising it to a particular value, it may contain a value which was previously assigned by a different program that has since released that piece of memory, or it may simply be a random value from when the computer was booted (iirc, PCs used to initialise all RAM to 0 on bootup because early versions of DOS required it, but new computers no longer do this). You can't assume the value will be zero, for instance.

0

Garbage value, e.g. in C, typically refers to the fact that if you just reserve memory, but never intialize it, it will hold random values, since it simply is not initialized yet (C doesn't do that for you automatically; it would just be overhead, and C is designed for as little overhead as possible). The random values in the memory are leftovers from whatever was in there before.

These previous values are left in there, because usually there is not much use in going around setting memory to zero - or any other value - that will later be overwritten again anway. Because for the general case, there is no use in reading uninitialized memory (except if you e.g. want to exploit possible security issues - see the special cases where memory is actually zeroed: Kernel zeroes memory?).

Community
  • 1
  • 1
codeling
  • 11,056
  • 4
  • 42
  • 71
  • just a little hint: there's an "up" arrow, and an "accept" button on the left side of each answer, with which you can show appreciation :D - beware though that you can only accept one answer! – codeling Aug 24 '12 at 10:28
  • LOL I am aware of that @nyarlathotep, I need some reputation points to do that, thats why I am using words to appreciate the help I am getting :D – Anshu Aug 24 '12 at 10:38