Reputation: 63
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.
Upvotes: 6
Views: 2359
Reputation: 507343
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);
}
Upvotes: 12
Reputation: 523724
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.
Upvotes: 17
Reputation: 76157
Because the first isn't declaring any space for the pointers in "b".
Upvotes: 0
Reputation: 108986
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.
Upvotes: 1
Reputation: 56986
This is because the second struct uses a flexible member array. Explanation of the sizeof result is in in Wikipedia.
Upvotes: 2