Zero-extent array members

Posted by on mai 01, 2007
Fără categorie

Some compilers, such as GCC or IBM’s compiler have a C extension that allows for a zero-extent array members of a structure to be declared:

struct inode{
//other members
char data[0];
};

They are very useful if you have a structure for a variable-length object. Until they are allocated the zero-extent members will not take up any memmory. When you have to allocate memory for such a structure, you can do it this way:

struct inode *ind = (struct inode*) malloc(sizeof(struct inode) + size_of_the_array);

You have to keep in mind that this is a language extension. The C99 standard only allows flexible array members, which are defined as

char array[];

(without the 0) and behave somewhat differently. You can find out more about the subject in the GCC Manual.

Lasă un răspuns

Adresa ta de email nu va fi publicată. Câmpurile obligatorii sunt marcate cu *

Acest site folosește Akismet pentru a reduce spamul. Află cum sunt procesate datele comentariilor tale.