c - For what reason there are 3 bytes more in structs? Can remove? -


struct things {     char foo[25];     int bar; };  struct morethings {     char morefoo[25];     int morebar;     int another; };  int main() {     printf("char[25] + int: %d | struct things: %d\n\n", sizeof(char[25]) + sizeof(int), sizeof(struct things));     printf("char[25] + int + int: %d | struct morethings: %d\n\n", sizeof(char[25]) + sizeof(int) + sizeof(int), sizeof(struct morethings));      return 0; } 

return:

char[25] + int: 29 | struct things: 32

char[25] + int + int: 33 | struct morethings: 36

i believe return of sizeof should same in both cases, struct have 3 bytes. reason happens?

can remove? can disrupt i'm doing, save in file structure.

it depends on compiler you're using, need like

#pragma pack(push)  struct things    {     char foo[25];     int bar;   }; #pragma pack(pop).  

check these links more information

  1. gnu compiler gcc
  2. microsoft compiler msdn

there gcc specific solution, is

struct __attribute__ ((__packed__)) things    {     char foo[25];     int bar;   }; 

Popular posts from this blog