1

Possible Duplicate:
Why does the C standard leave use of indeterminate variables undefined?

When a variable or pointer is not initialized why it holds a garbage value, is it some kind of a necessity for these entities to hold some sort of value to exist in the memory?

Community
  • 1
  • 1
Anshu
  • 13
  • 1
  • 3

5 Answers5

2

Only local variables are not initialized in C. Global variables or static ones are initialized.

The reason of not initializing them in the language specification is (or was) performance : the compiler don't need to emit machine code for implicit initialization of local values on the call stack. (in contrast, Java initializes all local variables)

My belief is that you should almost always initialize them (e.g. to 0 for integers, and to NULL for pointers; for aggregate like local arrays and structures, use memset). A good compiler would optimize almost always unneeded initialization. And if all your local variables are initialized, your code has a more reproducible behavior.

The GCC compiler (at least the recent versions e.g. 4.6) gives good warning about unitialized variables. I strongly suggest passing -Wall to GCC.

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
  • "The GCC compiler (at least the recent versions e.g. 4.6) gives good warning about unitialized variables" - and that's precisely why you shouldn't *needlessly* initialize things. So if I write `int i; if (condition1) { i = 3; } else if (condition2) { i = 5; } printf("%d\n", i);`, then gcc will warn me that `i` was conditionally uninitialized -- it might well be a bug in the code that I forgot to decide what to do in the `else` case. If you write `int i = 0;` in the first place, but `0` is not actually a meaningful value, then the code compiles and runs, but produces meaningless output `0`. – Steve Jessop Dec 14 '11 at 11:42
  • Yes, but the buggy code has a easily reproducible bug. If you don't initialize things, the bug is much less reproducible, so much harder to find. I believe that initializing locals is some kind of defensive programming. Of course, we should also add `assert`-s, comments, documentations.... – Basile Starynkevitch Dec 14 '11 at 11:45
  • Oh, you did used to need at least a certain level of optimization before you got the warnings. Not sure if that's still the case in 4.6 – Steve Jessop Dec 14 '11 at 11:45
  • 1
    I think it's defensive programming if there's a meaningful value you can initialize it to. But in cases where there isn't, setting a spurious value just gives you one less chance at finding the bug. I suppose I should pay lip service to the TDD rhetoric: well-tested code is completely bug free and therefore requires no compiler warnings at all ;-) But in practice, I'd like to find the bug at compile time, and that means I want the compiler to check for me that I really did initialize in every case with a value that I chose for the purpose. I don't want rogue zeros any more than garbage. – Steve Jessop Dec 14 '11 at 11:47
  • To be fair, I also have the habit of having 0 a [semi-] meaningful value, perhaps by starting with `assert (param != NULL)` several routines. – Basile Starynkevitch Dec 14 '11 at 11:51
  • I was misinterpreting this thing as some sort of Bug, I mean who wouldn't wonder why a garbage value, why not just a reserved memory location with no value, but now I got the Idea, Thanks for your time, Great help, I really appreciate it. – Anshu Dec 14 '11 at 11:52
1

A variable or a pointer (which is also a variable) actually is a name given to some location in computer's memory. That memory is a combination of bits and those bits can either be 0 or 1. Now when a variable is created, the bits of memory that it is assigned to will have some state, some of them will be 1 and some 0. This is what we call garbage as we dont know what the value of those bits is going to be and therefore we need to explicitly initialize these variables.

binW
  • 13,220
  • 11
  • 56
  • 69
0

Variables are (after all) locations in memory, no one will clean this location for you if you don't specify it.

For instance, if the variable is local variable (not static, in a function) then usually the allocation for it will only be to change the value of the stack pointer, without changing the values on the stack. In other cases, maybe a register will be used to hold the variable, and again, no one promise you that it will be cleaned.

MByD
  • 135,866
  • 28
  • 264
  • 277
0

No, pointers are just numbers to start with, and they need to be associated to valid memory locations using allocation primitives before they are referenced.

Dereferencing an unitialized pointer leads to either garbage, in the best of cases, but in the worst of cases they cause a segmentation violation and make the program crash.

fge
  • 119,121
  • 33
  • 254
  • 329
0

A pointer is just holding a memory address. when it was declared it shows the data present at that memory address.

it'll be extra work in c to assign a pointer to a proper value or NULL.

Azodious
  • 13,752
  • 1
  • 36
  • 71