Thread: Strange size of struct

  1. #1
    Registered User Bill Cosby's Avatar
    Join Date
    Sep 2004
    Posts
    11

    Question Strange size of struct

    Hi there,

    I have a question regarding structs, I have a struct with five members, 2 bytes + 4 bytes + 2 bytes + 2 bytes + 4 bytes, so this should mean my struct is 14 bytes large, the sizes of the members are correct, if I do a sizeof(member) then I get these numbers, however sizeof(my struct) is 16 bytes large, how come?

    Thanks in advance,
    Bill

    Update:
    Ok, if I do

    Code:
    struct test {
    unsigned short mem1;
    }
    Then my struct is 2 bytes large, which is fine.

    If I do
    Code:
    struct test {
    unsigned short mem1;
    unsigned long mem2;
    }
    then my struct is 8 bytes large, even though (unsigned long) is only 4 bytes, I am confused
    Somehow only the last long will get a 6 byte size, does anyone know why?
    I mean I have more than one struct in my program, and the others do have correct sizes.
    Last edited by Bill Cosby; 09-23-2008 at 02:09 AM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Search the Web for phrases like "struct padding" and "data structure alignment".
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User Bill Cosby's Avatar
    Join Date
    Sep 2004
    Posts
    11
    Cool thank you, I got it working with the pragma directive.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Well, before you get too excited about your solution, remember that there is reason for the padding in the first place, so it may not be desirable to pack the struct such that it is not padded, unless this packing is really necessary.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Link List math
    By t014y in forum C Programming
    Replies: 17
    Last Post: 02-20-2009, 06:55 PM
  2. Replies: 1
    Last Post: 12-03-2008, 03:10 AM
  3. Adventures in labyrinth generation.
    By guesst in forum Game Programming
    Replies: 8
    Last Post: 10-12-2008, 01:30 PM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  5. Bi-Directional Linked Lists
    By Thantos in forum C Programming
    Replies: 6
    Last Post: 12-11-2003, 10:24 AM