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.

Tags: ,

1st

New blog – Coder Tricks

Posted by Strainu | Filed under Geeks, My Projects, Personal, School

With the help of some colleagues, I just launched a new blog. It’s called Coder Tricks and it will contain programming tricks that the teachers don’t usually bother to tell you in programming courses. Hope you like it!

If you wish to offer any kind of feedback, you’re more than welcomed to contact us at codertricks@strainu.ro.