Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

value of allocated array not initialized

Tags:

c++

arrays

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?

like image 204
Jake Avatar asked Nov 28 '22 18:11

Jake


2 Answers

"...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.

like image 75
AnT Avatar answered Dec 16 '22 09:12

AnT


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.

like image 41
Prasoon Saurav Avatar answered Dec 16 '22 11:12

Prasoon Saurav