Reputation: 5848
Probably this is very dumb question. I'm very new to C. Could anyone please explain in simpler way why empty array has 0 byte in memory?
int shoot = 2; // 4 bytes
int zet[4] = {}; // 16 bytes
int raw[] = {}; // 0 bytes
Why variable raw takes 0 bytes from memory?
Upvotes: 5
Views: 3360
Reputation: 1485
When you declare zet
, you give the array a size.
When you declare raw
, you do not. When an array is declared with empty square braces, the size allocated for it by the compiler is determined by the number of elements in the initializer.
From a 2007 committee draft of the C standard:
EXAMPLE 2 The declaration
int x[] = { 1, 3, 5 };
defines and initializes x as a one-dimensional array object that has three elements, as no size was specified and there are three initializers.
In your case, you have int raw[] = {};
There are no initializers, so the array has no elements, and thus is of zero size.
Zero size arrays, however, are not necessarily portable, as they are not part of the C standard. This is compiler-dependent language extension.
Upvotes: 2