question about struct

This is a discussion on question about struct within the C++ Programming forums, part of the General Programming Boards category; I think it very easy but it makes me confused Code: struct Fileheader { unsigned short Type; // BM Identifier ...

  1. #1
    abu
    abu is offline
    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,796
    >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
    abu
    abu is offline
    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,796
    >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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,485
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

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, 02: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, 03:49 PM
  5. towers of hanoi problem
    By aik_21 in forum C Programming
    Replies: 1
    Last Post: 10-02-2004, 01:34 PM

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