Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Size of a struct with two void pointers is 4?

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.

like image 511
Alan J. Avatar asked Sep 16 '10 13:09

Alan J.


People also ask

What is the size of void pointers?

The size of void pointer varies system to system. If the system is 16-bit, size of void pointer is 2 bytes. If the system is 32-bit, size of void pointer is 4 bytes. If the system is 64-bit, size of void pointer is 8 bytes.

Why is sizeof void 1?

When you do pointer arithmetic adding or removing one unit means adding or removing the object pointed to size. Thus defining sizeof(void) as 1 helps defining void* as a pointer to byte (untyped memory address).

How do you determine the size of a struct byte?

If you want to manually count it, the size of a struct is just the size of each of its data members after accounting for alignment. There's no magic overhead bytes for a struct.

What does a void pointer contain?

A void pointer is a pointer that has no associated data type with it. A void pointer can hold address of any type and can be typecasted to any type.


Video Answer


3 Answers

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.

like image 199
kennytm Avatar answered Nov 06 '22 23:11

kennytm


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);
}
like image 35
Johannes Schaub - litb Avatar answered Nov 06 '22 23:11

Johannes Schaub - litb


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

like image 27
Alexandre C. Avatar answered Nov 06 '22 22:11

Alexandre C.