Thread: Varying Struct Sizes

  1. #1
    Magically delicious LuckY's Avatar
    Join Date
    Oct 2001
    Posts
    856

    Varying Struct Sizes

    Odd. I have a struct like this:
    Code:
    struct Old {
      int   a;
      char str[48];
      int   b;
      int   c;
      int   d;
    };
    and the sizeof() it is 64. Right. So I decided I needed to add a bool to it so I made the string 47 and added it, like so:
    Code:
    struct New {
      int a;
      char str[47];
      int b;
      int c;
      int d;
      bool x;
    };
    But the sizeof() the new struct is 68. Why? With the new struct it won't go back down to 64 until I decrease the size of str to 44.

    I'm compiling with Borland and think there just must be some details about structs that I am unaware of. Have you any clue?

  2. #2
    l'Anziano DavidP's Avatar
    Join Date
    Aug 2001
    Location
    Plano, Texas, United States
    Posts
    2,743
    bools are not one byte. bools are 32 bits, therefore 4 bytes, therefore making the struct 68 bytes and not 64.
    My Website

    "Circular logic is good because it is."

  3. #3
    Magically delicious LuckY's Avatar
    Join Date
    Oct 2001
    Posts
    856
    No they are not (depending on your compiler I suppose). A bool is 1 byte and a sizeof() confirms it. It's very odd because with the New struct, if the str size is 47, 46, or 45 the size of the struct is 68 (but it jumps down to 64 if the size of str is 44). There isn't a one-to-one relationship there for some reason.

  4. #4
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Its really all about byte alignment.

    Try changing it to:
    Code:
    struct New {
      int a;
      char str[47];
      bool x;
      int b;
      int c;
      int d;
    };
    And see what you get.

  5. #5
    Magically delicious LuckY's Avatar
    Join Date
    Oct 2001
    Posts
    856
    Brilliant. So, that is the reason... I have encountered this type of thing in the past but I just ignored it because it wasn't important.

    Would you care to elaborate a little more on this little quirk?

    Thanks. I see why you have such a good reputation.

  6. #6
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    It's just like he said, it has to do with data alginment. You can look it up and find information almost anywhere on the topic but it's not that interesting. If you are using MSVC you can change it by using

    Code:
     #pragma pack(push,1)
     //.. structs
     #pragma pack(pop)
    That will force one-byte boundaries. You generally only need to worry about this sort of thing when you need a very small memory footprint. If you're using another compiler look up in the documentation to see if similar functionality exists.
    "...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers

  7. #7
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Basically your cpu works more effiecently when it can uses address a certain way. Your compiler will try to do this as much as possible. The environment you are in wants things on even address.

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. What's wrong with my search program?
    By sherwi in forum C Programming
    Replies: 5
    Last Post: 04-28-2006, 09:57 AM
  3. Function validation.
    By Fhl in forum C Programming
    Replies: 10
    Last Post: 02-22-2006, 08:18 AM
  4. Search Engine - Binary Search Tree
    By Gecko2099 in forum C Programming
    Replies: 9
    Last Post: 04-17-2005, 02:56 PM
  5. what does this mean to you?
    By pkananen in forum C++ Programming
    Replies: 8
    Last Post: 02-04-2002, 03:58 PM