Thread: question about struct

  1. #1
    Registered User
    Join Date
    Mar 2003
    Posts
    11

    question about struct

    I think it very easy but it makes me confused
    Code:
    struct Fileheader
    { 
    unsigned short Type; // BM Identifier
    unsigned long  Size; // total length in bytes
    unsigned short Reserved1; // image id or revision 
    unsigned short Reserved2;
    unsigned long  OffBits;// offset to start of actual pixel data
    Fileheader()
    {
    Reserved1=0;
    Reserved2=0;
    }
    };
    int main()
    {
    	cout<<sizeof(Fileheader)<<endl;
    }
    The Output is 16 <---2+4+2+2+4=??
    I can't believe so I check again
    Code:
    struct Fileheader
    { 
    unsigned short Type; // BM Identifier
    unsigned short  Size; // total length in bytes
    unsigned short Reserved1; // image id or revision 
    unsigned short Reserved2;
    unsigned long  OffBits;// offset to start of actual pixel data
    Fileheader()
    {
    Reserved1=0;
    Reserved2=0;
    }
    };
    int main()
    {
    	cout<<sizeof(Fileheader)<<endl;
    }
    The output is 12=2+2+2+2+4...accurate but


    So it means that we can't have a struct having size of 14??

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >So it means that we can't have a struct having size of 14??
    It depends on how your implementation aligns memory as well as how structures are padded to meet those alignment requirements.
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Mar 2003
    Posts
    11
    You mean that b/c of I'm out of memory??I don't think so
    I want to load bitmap file into my program so the struct must have the length enough.
    so anyone can explain me
    The Beginner

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >You mean that b/c of I'm out of memory??
    No, I don't. I mean that structures are padded for alignment reasons. This changes the size of the structure instances. That is why you don't get the size that you expect and cannot get the size that you want. If your code requires that the size of a structure instance be 14 bytes, you must read the file manually and assign your input to the correct member.
    My best code is written with the delete key.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Depending on your compiler, you could try
    Code:
    #pragma pack(1)
    struct Fileheader
    { 
      unsigned short Type; // BM Identifier
      unsigned short  Size; // total length in bytes
      unsigned short Reserved1; // image id or revision 
      unsigned short Reserved2;
      unsigned long  OffBits;// offset to start of actual pixel data
    };
    #pragma pack()
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help with linked list sorting function
    By Jaggid1x in forum C Programming
    Replies: 6
    Last Post: 06-02-2009, 02:14 AM
  2. Struct question... difference between passing to function...
    By Sparrowhawk in forum C++ Programming
    Replies: 6
    Last Post: 02-23-2009, 03:59 PM
  3. Looking for a way to store listbox data
    By Welder in forum C Programming
    Replies: 20
    Last Post: 11-01-2007, 11:48 PM
  4. struct question
    By caduardo21 in forum Windows Programming
    Replies: 5
    Last Post: 01-31-2005, 04:49 PM
  5. towers of hanoi problem
    By aik_21 in forum C Programming
    Replies: 1
    Last Post: 10-02-2004, 01:34 PM