3
struct xyz {
    int a;
    int b;
    char c[0];
};
struct xyz x1;
printf("Size of structure is %d",sizeof(x1));

Output: 8 why isn't the size of structure 9 bytes? Is it because the character array declared is of size 0?

Keith Thompson
  • 254,901
  • 44
  • 429
  • 631
Kulks
  • 63
  • 2
  • 4
  • 3
    yes it is because of that – aaronman Aug 08 '13 at 21:34
  • Here is a similar question. You will likely find the answers relative to your question http://stackoverflow.com/questions/4690718/zero-length-arrays – JaredPar Aug 08 '13 at 21:39
  • This isn't valid C, for several reasons. – Kerrek SB Aug 08 '13 at 21:43
  • @KerrekSB Perhaps not by the standard, but it is a pretty common `gcc` (and maybe others) extension... – twalberg Aug 08 '13 at 21:58
  • @twalberg: C99 and C11 allow something similar whereby you say `char c[];`... – Kerrek SB Aug 08 '13 at 22:01
  • @KerrekSB I realize that, but I don't think `gcc` and other compilers have yet (fully) deprecated the zero-length-array extension, and it's still present in a lot of code... – twalberg Aug 08 '13 at 22:07
  • @KerrekSB: Yes, it is valid C. In the terms of the C standard, it is code that may be in a *conforming* program but not in a *strictly conforming* program. C is designed to be extended, and programs that use extensions may be conforming programs. – Eric Postpischil Aug 08 '13 at 23:15

4 Answers4

5

Zero-length arrays are not in the standard C, but they are allowed by many compilers.

The idea is that they must be placed as the very last field in a struct, but they don't occupy any bytes. The struct works as a header for the array that is placed just next to it in memory.

For example:

struct Hdr
{
    int a, b, c;
    struct Foo foos[0]
};

struct Hdr *buffer = malloc(sizeof(struct Hdr) + 10*sizeof(Foo));
buffer->a = ...;
buffer->foos[0] = ...;
buffer->foos[9] = ...;

The standard way to do that is to create an array of size 1 and then substracting that 1 from the length of the array. But even that technique is controversial...

For more details and the similar flexible array member see this document.

rodrigo
  • 94,151
  • 12
  • 143
  • 190
3

Your array of characters has a length of 0 and hence the size of c is 0 bytes. Therefore when your compiler allocated a block of memory for that structure it only considers both integers and since you are on a 32-bit environment (assuming so from your result) the size of the structure is 8 bytes.

Remark: You can still access the field c without any compiler warnings (compiled with gcc) however it will be some garbage value.

Jacob Pollack
  • 3,703
  • 1
  • 17
  • 39
0

In this case:

int a;//4

int b;//4

char c[0] ; 0

So it is 8.

And The sizeof never return 9 in you struct even if you give a size to char c[];

int a;//4  byte
int b;//4   byte
char c[1];// one byte but it should alignment with other. 

just in this way:

^^^^
^^^^
^~~~     //for alignment

So, sizeof return 12 not 9

Lidong Guo
  • 2,817
  • 2
  • 19
  • 31
0

An array of length 0 is actually not permitted in standard C, but apparently your compiler supports it as an extension.

It's one way of implementing the so-called "struct hack", explained in question 2.6 of the comp.lang.c FAQ.

Because C implementations typically don't do bounds checking for arrays, a zero-element array (or in a more portable variant, a one-element array) gives you a base address for an array of arbitrary size. You have to allocate, typically using malloc, enough memory for the enclosing struct so that there's room for as many array elements as you need:

struct xyz *ptr = malloc(sizeof *ptr + COUNT * sizeof (char));

C99 added a new feature, "flexible array members", that does the same thing without specifying a fake array size:

 struct xyz {
     int a;
     int b;
     char c[];
 };
Keith Thompson
  • 254,901
  • 44
  • 429
  • 631
  • To stay compatible with both `[0]` and `[1]`, we could do `malloc(sizeof *ptr - sizeof ptr->c + COUNT * sizeof (char));` or even `malloc(sizeof *ptr - sizeof ptr->c + COUNT * sizeof ptr->c[0]);` – glglgl Aug 08 '13 at 21:45