1st
MAY

Zero-extent array members

Posted by Strainu | Filed under C

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.

Share and Enjoy:
  • Facebook
  • Twitter
  • Identi.ca
  • email
  • Add to favorites
  • Digg
  • StumbleUpon

Tags: ,

Leave a Reply