#include <stdio.h>
int main()
{
int a[0],b[4][0];
printf("%d %d ",sizeof(a),sizeof(b));
}
//output
0 0
what is the significance of a[0]
, why also 2d array of size 0 is allowed?
Zero-length arrays, also known as flexible arrays, are used to implement variable-length arrays, primarily in structures. That's a little confusing, so let's look at an example. Say you wanted a structure to represent an email: Yes, we're missing a lot of fields. This is just an example; bear with me.
Although the size of a zero-length array is zero, an array member of this kind may increase the size of the enclosing type as a result of tail padding. The offset of a zero-length array member from the beginning of the enclosing structure is the same as the offset of an array with one or more elements of the same type.
This array is immutable (it can't be changed), and can be shared throughout the application.
It will create an empty array object. This is still a perfectly valid object - and one which takes up a non-zero amount of space in memory. It will still know its own type, and the count - it just won't have any elements.
Neither C nor C++ allow arrays of zero length, so your program is ill-formed.
(E.g. C++11, 8.3.4/1: "[the array size] shall be greater than zero".)
(As one point of rationale: An array of zero length would be tricky and confusing to reconcile with the requirement that each object have a unique address.)
As @sidyll points out, zero-length arrays are available as an extension in GCC.
You'll find your answer in The GCC manual
If you are using c99
- Flexible array members are written as contents[] without the 0.
- Flexible array members have incomplete type, and so the sizeof operator may not be applied. As a quirk of the original implementation of zero-length arrays, sizeof evaluates to zero.
- Flexible array members may only appear as the last member of a struct that is otherwise non-empty.
- A structure containing a flexible array member, or a union containing such a structure (possibly recursively), may not be a member of a structure or an element of an array. (However, these uses are permitted by GCC as extensions.
And of course, how they can be useful:
Zero-length arrays are allowed in GNU C. They are very useful as the last element of a structure which is really a header for a variable-length object:
struct line { int length; char contents[0]; }; struct line *thisline = (struct line *) malloc (sizeof (struct line) + this_length); thisline->length = this_length;
There is not much use as given in your example, but zero size arrays were frequently used in structures where the last element was dynamically sized:
struct {
int some_fixed_data [N_FIXED];
float more_fixed_size_data [F_FIXED];
int n_variable_elements;
long variable_elements [0]; // allocated based on item above
} s;
int curlen = sizeof s + sizeof long * s.n_variable_elements;
The use of a zero length array is:
1) variable_elements
has an address (despite someone's answer)
2) it also has array semantics
3) computing the dynamic size of the array is simplified
Unfortunately, some compilers (MSC) would throw a hissy fit over such a construction and force a lesser, appeasing, technically incorrect reformulation:
struct {
int some_fixed_data [N_FIXED];
float more_fixed_size_data [F_FIXED];
int n_variable_elements;
long variable_elements [1]; // allocated based on item above
} s;
int curlen = sizeof s + sizeof long + (s.n_variable_elements - 1);
Think of a zero size array as a placeholder. There is little need to do that anymore, unless you are forced to use C
which is the case on many embedded environments.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With