Rafael D
Rafael D

Reputation: 65

How is an array of size 0 stored in memory?

If I have an array of only one element, we can say it is the same as a pointer. But how's an array of size zero represented in the memory?

What is happening when I declare a variable int * table[0]?

Upvotes: 5

Views: 745

Answers (3)

dbush
dbush

Reputation: 223972

Strictly speaking, an array is not allowed to have a size of 0. This is dictated in section 6.7.6.2p1 of the C standard:

In addition to optional type qualifiers and the keyword static , the [ and ] may delimit an expression or * . If they delimit an expression (which specifies the size of an array), the expression shall have an integer type. If the expression is a constant expression, it shall have a value greater than zero. The element type shall not be an incomplete or function type. The optional type qualifiers and the keyword static shall appear only in a declaration of a function parameter with an array type, and then only in the outermost array type derivation

Some compilers do support this as an extension, for example GCC:

Declaring zero-length arrays is allowed in GNU C as an extension. A zero-length array can be useful as the last element of a structure that 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;

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. The alignment of a zero-length array is the same as the alignment of its elements.

Upvotes: 1

codeandkey
codeandkey

Reputation: 69

Zero-length arrays are not technically supported by the C standard. However some compilers such as gcc allow them as an extension.

They are a pre-C99 way of creating flexible array members, as shown here:

typedef struct {
    int len;
    int contents[0];
} arr;

Then, you can initialize as much space as you'd want for the array at the end:

arr* a = malloc(sizeof(*a) + sizeof(a->contents) * 10); /* for a length-10 array */
a->len = 10;

Upvotes: 2

bolov
bolov

Reputation: 75717

If I have an array of only one element, we can say it is the same as a pointer

No we cannot. Arrays and pointers are different types and they are represented differently internally. This is true regardless of the array size. Now it is also true that in certain situations (most of the situations actually) an array decays to a pointer to it's first element.

Arrays of size 0 are illegal as per standard, however some major compilers like gcc allow them as an extension.

Read this question to see the difference between internal representations of arrays and pointers: Difference between dereferencing pointer and accessing array elements

Upvotes: 4

Related Questions