6

I don't understand why

struct e{
    void * a;
    void * b[];
}

has sizeof(e) == 4 while

struct f{
    void * a;
    void * b;
}

has sizeof(f) == 8.

Alan J.
  • 63
  • 3
  • A [discussion of the legality of the "struct hack" in ansi c](http://stackoverflow.com/questions/3711233/). Searching on "struct hack" on So doesn't return much of interest right now, but that is the phrase you are looking for. – dmckee --- ex-moderator kitten Sep 16 '10 at 14:25

5 Answers5

17
struct e{
    void * a;
    void * b[];
//          ^^
}

The [] in a struct makes b a C99 "flexible array member". Thus sizeof(e) will count the size of a only, which is 4.

From C99 §6.7.2.1/16:

As a special case, the last element of a structure with more than one named member may have an incomplete array type; this is called a flexible array member. In most situations, the flexible array member is ignored. In particular, the size of the structure is as if the flexible array member were omitted except that it may have more trailing padding than the omission would imply.

However, when a . (or ->) operator has a left operand that is (a pointer to) a structure with a flexible array member and the right operand names that member, it behaves as if that member were replaced with the longest array (with the same element type) that would not make the structure larger than the object being accessed; the offset of the array shall remain that of the flexible array member, even if this would differ from that of the replacement array. If this array would have no elements, it behaves as if it had one element but the behavior is undefined if any attempt is made to access that element or to generate a pointer one past it.

Community
  • 1
  • 1
kennytm
  • 510,854
  • 105
  • 1,084
  • 1,005
  • 1
    Does that mean i can't access it? – Alan J. Sep 16 '10 at 14:01
  • @Alan: No, you can still access `b`. – kennytm Sep 16 '10 at 14:03
  • @Alan: no; it just means that sizeof() returns the result you observe. – Jonathan Leffler Sep 16 '10 at 14:03
  • @Alan: Yes. You need a C99 compiler, and you need to allocate space for the structure beyond the `sizeof` the structure. If you put it on the stack accessing it will be UB. – Billy ONeal Sep 16 '10 at 14:03
  • By your comments and the "sizeof" entry on Wikipedia i now grasp it. Thanks! – Alan J. Sep 16 '10 at 14:06
  • If one has a `struct FOO {int32 x,y;}; struct BAR {int32 a,b; FOO dat[];}, and one allocates a `BAR *boz` with 36 bytes, could one legally access `boz->dat[3].x`? Although `boz->dat[3]` won't fit entirely within the allocated space, `boz->dat[3].x` should start 32 bytes after the start of `boz`, and thus should fit within the allocated space. – supercat Oct 06 '12 at 08:59
12

The second in the first struct is not a pointer, but a FAM - flexible array member. It is used when you have a long buffer and place an e at the start of that buffer. You can then index the remaining memory that follow the e object using that FAM and treat that memory as an array of void*.

The Standard says (emphasis by me)

As a special case, the last element of a structure with more than one named member may have an incomplete array type; this is called a flexible array member. In most situations, the flexible array member is ignored. In particular, the size of the structure is as if the flexible array member were omitted except that it may have more trailing padding than the omission would imply.

For example, the following code outputs 1 for the struct without, but 4 for the struct with the FAM on GCC, because to access integers the FAM need to be properly aligned (on a 4 byte boundary in this example)

struct A {
  char a;
};

struct B {
  char a;
  int flex[];
};

int main() {
  printf("sizeof A: %d\nsizeof B: %d\n", 
         (int)sizeof(struct A),
         (int)sizeof(struct B)
    );

  struct B *b = malloc(sizeof *b + sizeof(int[3]));
  b->a = 'X';
  b->flex[0] = 1;
  b->flex[1] = 2;
  b->flex[2] = 3;
  free(b);
}
Johannes Schaub - litb
  • 496,577
  • 130
  • 894
  • 1,212
  • Since you compiled the contents in various posts and added an example, i accepted your answer over the previous one. – Alan J. Sep 16 '10 at 14:25
2

This is because the second struct uses a flexible member array. Explanation of the sizeof result is in in Wikipedia.

Alexandre C.
  • 55,948
  • 11
  • 128
  • 197
1

void * b[]; is invalid in C89, so it means you're using a C99 compiler.

C99 introduced a means to define the "struct hack": it's now called "flexible array member" and before it is allocated memory, its size is 0.

pmg
  • 106,608
  • 13
  • 126
  • 198
0

Because the first isn't declaring any space for the pointers in "b".

fortran
  • 74,053
  • 25
  • 135
  • 175