Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the significance of a 0-length array in C? [duplicate]

Tags:

arrays

c

#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?

like image 272
Utkarsh Srivastav Avatar asked Dec 24 '11 16:12

Utkarsh Srivastav


People also ask

What is a zero-length array?

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.

Can an array have 0 length?

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.

Is an array of length 0 immutable?

This array is immutable (it can't be changed), and can be shared throughout the application.

Can you initialize an array of size 0?

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.


3 Answers

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.

like image 127
Kerrek SB Avatar answered Nov 02 '22 08:11

Kerrek SB


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;
like image 35
Anil Arya Avatar answered Nov 02 '22 07:11

Anil Arya


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.

like image 39
wallyk Avatar answered Nov 02 '22 06:11

wallyk