4

If I have:

int array[20];

What is the value of array[0] if nothing has been initialized there yet? Is there a way to check if it has been initialized?

Mateen Ulhaq
  • 24,552
  • 19
  • 101
  • 135
Jake
  • 2,877
  • 8
  • 43
  • 62
  • Related is that [reading uninitialized variables is undefined behavior](http://stackoverflow.com/questions/4259885/why-do-i-see-strange-values-when-i-print-uninitialized-variables/4259991#4259991), so don't even think about doing it. – GManNickG May 04 '11 at 05:46

6 Answers6

16

"...suppose I have..." You have it where?

If you have something like this in namespace scope, then it is an object with static storage duration, which is always zero-initialized.

If you have something like this in local scope, then it is an object with automatic storage duration, which is not initialized at all. The value of array[0] is unpredictable. And no, there's no way to tell whether something has been deliberately initialized or not.

AnT stands with Russia
  • 312,472
  • 42
  • 525
  • 765
4

What is the value of array[0] supposing nothing has been initialized there yet?

It depends whether the array is defined in the file scope or in the function scope. In function scope array elements would contain unspecified values whereas in file scope they would be zero initialized.

Prasoon Saurav
  • 91,295
  • 49
  • 239
  • 345
  • File scope would make it: static int array[20] – Pepe May 04 '11 at 05:31
  • @Peter R.: What exactly are you trying to say by this? – AnT stands with Russia May 04 '11 at 05:32
  • 1
    @Peter R.: You can declare an array in file scope as `int array[20]` without any extra `static`. Whether you need `static` there or not is a completely different issue that depends on your intent. – AnT stands with Russia May 04 '11 at 05:38
  • @AndreyT wouln't that just make it a global variable and NOT initialize it? – Pepe May 04 '11 at 05:41
  • @Peter: The problem here is all these informal terms. If something has static storage duration, it is zero initialized. "Globals", "file scope", "statics" all have static storage duration. Easier to simply say: "if it's automatically allocated, it's uninitialized, otherwise it's zero-initialized". – GManNickG May 04 '11 at 05:43
3

It's basically garbage or whatever value was in that particular memory spot. You can't detect if it's been initialized no.

Pepe
  • 6,360
  • 5
  • 27
  • 29
0

It will be in so-called indeterminate state, which on most implementations means whatever was in that memory at the moment memory was occupied by the array - that's typically called garbage.

sharptooth
  • 167,383
  • 100
  • 513
  • 979
0

It's undefined, depends on the compiler and release flavour : some compiler will fill it with a pattern in debug, but won't do it in release. In any case, relying on the compiler and trying to check it instead of properly initializing is a bad practice and will probably lead you to major issues.

Bruce
  • 7,094
  • 1
  • 25
  • 42
0

Is there a way to check if it has been initialized?

There's no need to check.Unless you explicitly initialize it, it stays uninitialized.

Kien Truong
  • 11,179
  • 2
  • 30
  • 36