size of structure

This is a discussion on size of structure within the C++ Programming forums, part of the General Programming Boards category; Hi, I have 2 structures like this: Code: struct A { char i; char j; }; Code: struct B { ...

  1. #1
    Registered User
    Join Date
    Feb 2010
    Location
    PL
    Posts
    1

    Question size of structure

    Hi,
    I have 2 structures like this:

    Code:
    struct A
    {
     char i;
     char j;
    };
    Code:
    struct B
    {
     char i;
     char j;
     int k;
    };
    and it is quite obvious that sizeof(A) == 2, but why sizeof(B) == 8?
    Does anybody know where I could read something about how does compiler allocates memory?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    23,772
    Quote Originally Posted by Leone
    and it is quite obvious that sizeof(A) == 2, but why sizeof(B) == 8?
    Probably because two bytes of padding are inserted after j so that k can be aligned along a word boundary.

    Quote Originally Posted by Leone
    Does anybody know where I could read something about how does compiler allocates memory?
    Search the Web for say, data structure alignment. Note that this is indeed implementation defined.
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Generic heapsort
    By Sephiroth1109 in forum C Programming
    Replies: 15
    Last Post: 12-07-2007, 05:14 PM
  2. Replies: 5
    Last Post: 02-14-2006, 08:04 AM
  3. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 09:33 AM
  4. Changing a Structures Members array size
    By Xei in forum C++ Programming
    Replies: 1
    Last Post: 11-07-2002, 06:45 PM

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