sizeof(Header) giving wrong result

This is a discussion on sizeof(Header) giving wrong result within the C Programming forums, part of the General Programming Boards category; I have defined a struct like given but sizeof function gives one more than the actual size that is 13 ...

  1. #1
    Registered User
    Join Date
    May 2008
    Posts
    134

    Red face sizeof(Header) giving wrong result

    I have defined a struct like given but sizeof function gives one more than the actual size that is 13 why??
    Can You plz help me.
    Code:
    typedef struct {
            char type[3];
            char code[3];
            unsigned short int width;
            unsigned short int height;
            char flags;
            char bgColorI;
            char pARatio;
    }   HEADER;                                                   // size is 13
    
    int main(int argc,char **argv)
    {
             printf("%u", sizeof(HEADER));   // it gives 14 why?? thats is my question
    }

  2. #2
    Ex scientia vera
    Join Date
    Sep 2007
    Posts
    478
    Because your system pads it so it's a multiple of 2 in this case.

    However, if you use the packed attribute, it gives the correct size.

    Code:
    typedef struct {
            char type[3];
            char code[3];
            unsigned short int width;
            unsigned short int height;
            char flags;
            char bgColorI;
            char pARatio;
    } __attribute__((packed)) HEADER;                                                   // size is 13
    
    int main(int argc,char **argv)
    {
             printf("%u", sizeof(HEADER));   // it gives 14 why?? thats is my question
    }

  3. #3
    Registered User
    Join Date
    May 2008
    Posts
    134
    Thanx it worked if You don mind can u help me a bit more
    Code:
    typedef struct {
            char type[3];
            char code[3];
            unsigned short int width;
            unsigned short int height;
            char flags;
            char bgColorI;
            char pARatio;
    } __attribute__((packed)) HEADER;                                                 
    
    int main(int argc,char **argv)
    {
            HEADER header;
             printf("%u", sizeof(HEADER));
     
             //here one block of HEADER type is accessed from a file viz. fread(&header,13,1,fptr);
       
             printf("%d",header.flags); //this gives -10 can you tell me the bitwise values of this 8 bits
    }
    Last edited by kapil1089thekin; 05-05-2008 at 08:36 AM. Reason: came with a new problem

  4. #4
    Ex scientia vera
    Join Date
    Sep 2007
    Posts
    478
    Quote Originally Posted by kapil1089thekin View Post
    Thanx it worked if You don mind can u help me a bit more
    Code:
    typedef struct {
            char type[3];
            char code[3];
            unsigned short int width;
            unsigned short int height;
            char flags;
            char bgColorI;
            char pARatio;
    } __attribute__((packed)) HEADER;                                                 
    
    int main(int argc,char **argv)
    {
            HEADER header;
             printf("%u", sizeof(HEADER));
     
             //here one block of HEADER type is accessed from a file viz. fread(&header,13,1,fptr);
       
             printf("%d",header.flags); //this gives -10 can you tell me the bitwise values of this 8 bits
    }
    1111 0110

    Try googling for the two's complement.

  5. #5
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,274
    Quote Originally Posted by kapil1089thekin View Post
    I have defined a struct like given but sizeof function gives one more than the actual size that is 13 why??
    If you put a bunch of wooden beams together to create a house, do you expect the volume of the house to be equal to the volume of the wooden beams?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Something is wrong with my file reverse program
    By abh!shek in forum C Programming
    Replies: 17
    Last Post: 05-21-2008, 12:15 PM
  2. what's wrong with this?
    By jackstify in forum C++ Programming
    Replies: 5
    Last Post: 12-18-2007, 09:40 AM
  3. String Class
    By BKurosawa in forum C++ Programming
    Replies: 117
    Last Post: 08-09-2007, 02:02 AM
  4. what is wrong here?
    By The_Nymph in forum C Programming
    Replies: 3
    Last Post: 04-20-2002, 09:57 AM
  5. reverse a singly linked list
    By ivandn in forum C Programming
    Replies: 15
    Last Post: 12-18-2001, 05:33 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21