Strange size of struct

This is a discussion on Strange size of struct within the C Programming forums, part of the General Programming Boards category; Hi there, I have a question regarding structs, I have a struct with five members, 2 bytes + 4 bytes ...

  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
    21,310
    Search the Web for phrases like "struct padding" and "data structure alignment".
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    21,310
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,411
    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, 05:55 PM
  2. Replies: 1
    Last Post: 12-03-2008, 02: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, 09:33 AM
  5. Bi-Directional Linked Lists
    By Thantos in forum C Programming
    Replies: 6
    Last Post: 12-11-2003, 09:24 AM

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