Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

zero length arrays vs. pointers

Tags:

EDIT: apparently some of this isn't allowed/has changed in various C standards. For my own hypothetical benefit, let's pretend we're using gcc test.c with no standard or warning options.

In particular I'm looking at the under-the-hood specifics. I've added my current understanding. Am I right?

char **c1;   //Size for a pointer is allocated on the stack. sizeof(c1) == sizeof(void*) char *c2[0]; //Nothing is allocated on the stack.  sizeof(c2) == 0 

is there some other difference between these two cases I'm not aware of (besides sizeof)?

struct a {    int i;    char c[0]; //sizeof(a) is sizeof(int)?  a.c == (&i)+1? }; 

As I understand it, this is typically used for variable length arrays at the end of structures. But what about

struct b {    char *c[0] //sizeof(b) is 0?  where does c point? };  int j; struct b myb; //myb.c == (&j)+1 == $esp? 

Furthermore, how is the address of a zero length array known if space for its pointer is never allocated anywhere? I suppose the same way a regular array's address is known, but I'm struggling to wrap my mind around it at the moment.

like image 704
jdizzle Avatar asked Mar 09 '09 18:03

jdizzle


People also ask

Are pointers better than arrays?

The main difference between Array and Pointers is the fixed size of the memory block. When Arrays are created the fixed size of the memory block is allocated. But with Pointers the memory is dynamically allocated.

Which is faster pointer or array?

Why ? pointers because it is direct memory access followed by dereferencing array - add current index to base address then dereferencing.

Can an array have zero-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.

Are pointers slower than arrays?

"The pointer version will in general be faster" means that in most cases it's easier for the compiler to generate more efficient code having a pointer (which just needs to be dereferenced) than having an array and subscript (which means that the compiler needs to shift the address from the start of the array).


1 Answers

The only time I've ever seen zero-length arrays actually used is when you want to have a variable length structure.

As in this example taken from here

    struct line {        int length;        char contents[0];      };       struct line *thisline = (struct line *)        malloc (sizeof (struct line) + this_length);      thisline->length = this_length; 

You don't want to use a pointer in the above struct as you want the contents to be part of the allocated memory of the structure.

If you do a sizeof on the above struct it doesn't include any space for the contents. I'm not sure if this ever made it into a standard, it gives warnings on various compilers.

like image 136
Andrew Barrett Avatar answered Nov 08 '22 15:11

Andrew Barrett