-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.

3
  • 2
    bool is not a C type. How is it defined? Commented Mar 14, 2012 at 5:16
  • Yea it is pretty much, didn't come across it in my search, got the answer though, thanks Commented Mar 14, 2012 at 5:23
  • 1
    @leppie: bool was not a standard type in C89, but it was in C99 after you #include <stdbool.h> (and _Bool is a type in C99 at all times). Commented Mar 18, 2012 at 21:22

5 Answers 5

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.

Sign up to request clarification or add additional context in comments.

Comments

0

Note That bool size on x64 is 2 bytes

Comments

0

You may find your answer from here or here.

Please check What packing? default packing (8)

Comments

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?

Comments

-1

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

1 Comment

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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.