-1

Possible Duplicate:
Why isn't sizeof for a struct equal to the sum of sizeof of each member?

I'm trying to parse ID3v2 tags, at least get important data like artist, title, and album, but I'm having trouble setting up the necessary structures.

struct ID3v2_frame{
  char id[4];
  unsigned int size;
  bool flags[2];
};
ID3v2_frame frame;
cout<<sizeof(frame)<<endl;

It's a problem with how I'm setting up a 4 byte int I think. When I output sizeof(frame) it outputs 12, my intended output is 10. I'm running on a 64 bit linux machine.

Community
  • 1
  • 1
carboncomputed
  • 1,630
  • 3
  • 20
  • 42

5 Answers5

0

Please check in your main program:

sizeof(bool) //should return 2 my guess
sizeof(char) //should return 1
sizeof(unsigned int) //should return 4

Every compiler can have different sizes for data types.

hrishikeshp19
  • 8,838
  • 26
  • 78
  • 141
0

Note That bool size on x64 is 2 bytes

Muhannad A.Alhariri
  • 3,702
  • 4
  • 30
  • 46
0

You may find your answer from here or here.

Please check What packing? default packing (8)

Community
  • 1
  • 1
Divyang Mithaiwala
  • 161
  • 1
  • 1
  • 3
0

It is because of structure packing. Compiler would make the size of structure a multiple of 4 bytes. If you do not want this to happen and see size as 10 bytes in your case, use __attribute__((packed))

struct ID3v2_frame{
char id[4];
unsigned int size;
bool flags[2];
}__attribute__((packed));

Note that this might affect the performance of the program. See this SO question:

Can __attribute__((packed)) affect the performance of a program?

Community
  • 1
  • 1
Sanish
  • 1,699
  • 1
  • 12
  • 21
-1

My guess would be the terminator character in the flags and id array.

joebro
  • 581
  • 3
  • 11
  • I seem to remember in my data structures class when you declare in array, the compiler always throws in a /n at the end to let the compiler know the string is done. So id would look like `id = [],[],[],[],/n` but i could be way off base too. – joebro Mar 14 '12 at 14:52