Packing a struct

This is a discussion on Packing a struct within the C++ Programming forums, part of the General Programming Boards category; Quick question. If I have the following struct: Code: struct Wombat { unsigned int Foo; char Bar; } Can I ...

  1. #1
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718

    Packing a struct

    Quick question. If I have the following struct:

    Code:
    struct Wombat
    {
       unsigned int Foo;
       char         Bar;
    }
    Can I be certain (assuming the int datatype is 4 bytes) that sizeof(Wombat) will always equal 5, or is there a possibility that the compiler may pad it out to 8 bytes to align it to word boundaries?
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  2. #2
    Supermassive black hole cboard_member's Avatar
    Join Date
    Jul 2005
    Posts
    1,709
    I'm pretty sure it'll pad based on what I think I know about structures

    EDIT: And why of all things did you call it Wombat?
    EDIT2: And I know that it's just a test struct. Right?
    Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.

    - Mike McShaffry

  3. #3
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    Hmm, in that case, if I had a struct that was 12 bytes, I guess I'd have to worry about 64-bit compilers padding it out to 16 bytes to align it on dword boundaries as well. Bah.

    To answer your questions:

    1) I like wombats.
    2) Yep, just a test struct, as is demonstrated by the fact that I left out the trailing semicolon after the closing brace.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  4. #4
    Supermassive black hole cboard_member's Avatar
    Join Date
    Jul 2005
    Posts
    1,709


    I should've noticed that. Meh.
    I'm wiki'ing Wombat since I have no idea what it is. Well I do, sort of, I'm just not sure.

    EDIT: Awesome. Someone thought it'd be funny to cross-breed a cat and a pig. Ouch.
    Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.

    - Mike McShaffry

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    I think you can use #pragma to align structures to x bytes, at least for most compilers. (I was looking at the SDL header begin_code.h earlier today.) Try googling it.

    Or better yet don't rely on five byte structures.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  6. #6
    Registered User Micko's Avatar
    Join Date
    Nov 2003
    Posts
    712
    Yes, they right compiler will probably pad structures. You may want to try this:
    Code:
    #include <cstdlib>
    #include <iostream>
    #pragma pack(1)
    using namespace std;
    
    struct test
    {
        int x;
        char y;
    };
    
    int main ( ) 
    {
        cout << sizeof(test);
        system("pause");
        return 0;
    }
    I test it on Win XP machine, Dev-Cpp.

    Try google it for details
    Gotta love the "please fix this for me, but I'm not going to tell you which functions we're allowed to use" posts.
    It's like teaching people to walk by first breaking their legs - muppet teachers! - Salem

  7. #7
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,451
    > Can I be certain (assuming the int datatype is 4 bytes) that sizeof(Wombat) will always equal 5
    You can't be certain of anything about the size of a struct, except that it is always at least the size of the sum of it's members. Compilers are free to add internal and trailing padding to preserve alignment (though they can't add anything before the first member).

    Compiler extensions like #pragma pack() are not universally portable, it may be ignored completely, or it may pack something else instead.

    > 1) I like wombats.
    I wouldn't play wom with anything else
    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. Converting from C to C++
    By Taka in forum C++ Programming
    Replies: 5
    Last Post: 04-08-2009, 02:16 AM
  2. Looking for constructive criticism
    By wd_kendrick in forum C Programming
    Replies: 16
    Last Post: 05-28-2008, 09:42 AM
  3. newbie needs help with code
    By compudude86 in forum C Programming
    Replies: 6
    Last Post: 07-23-2006, 08:54 PM
  4. Function validation.
    By Fhl in forum C Programming
    Replies: 10
    Last Post: 02-22-2006, 07:18 AM
  5. Search Engine - Binary Search Tree
    By Gecko2099 in forum C Programming
    Replies: 9
    Last Post: 04-17-2005, 02:56 PM

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