Trouble with sizeof

This is a discussion on Trouble with sizeof within the C++ Programming forums, part of the General Programming Boards category; The following code:- Code: class Box { public: int height; double length; double breadth; }; cout << sizeof(Box); I would ...

  1. #1
    Registered User
    Join Date
    Aug 2001
    Posts
    84

    Trouble with sizeof

    The following code:-

    Code:
    class Box
    {
    	public:
    	int height; 
    	double length;
    	double breadth;	
    };
    
     cout << sizeof(Box);
    I would expect to output 20 . double is 8 bytes*2=16 + 4 for
    an int =20 but the result i am getting is 24 . Can anyone explain why?

  2. #2
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    because a class has a function pointer table. 4 bytes = long doesn't it? I think it does. so the long is a pointer to the function pointer table. Can anyone verify that?

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,558
    > Can anyone explain why?
    Structures are padded to the alignment of the most restrictive type within your structure.

    Your struct contains a double, so the whole struct needs to be a multiple of 8 in size (I guess from your figures). If you try with ints and chars, it will probably be rounded up to the next multiple of 4 say.

    This is so that if you have an array of them (for example), all the members of the structure will be accessed efficiently, no matter which array element they are in.

  4. #4
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    but 20 is a multiple of 4.

  5. #5
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    I'm pretty sure that no matter what you put in a class, you have an extra 4 bytes. I've run tests on this because I saw the same thing. Try it out by changing what's in there.

  6. #6
    Registered User
    Join Date
    Aug 2001
    Posts
    84
    Let me get this straight so the moment you have a double in
    your class the byte size must be divisable by 8 . Is that right?

  7. #7
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,558
    > so the moment you have a double in
    > your class the byte size must be divisable by 8
    If the alignment of doubles on your machine is 8, then yes.

    doubles are already 8 bytes in size, so a class containing only doubles would be a multiple of 8 anyway.

  8. #8
    Unregistered
    Guest
    this is wierd. I ran tests with chars and longs in a class. When there was just 3 chars in the class sizeof() returned 3. When there was a long, sizeof returned 4. so far makes sense. But when there was a long and an char, sizeof returned 8.

    Salem, how come it doesn't round up when there are just chars?

  9. #9
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    ^ that was me

  10. #10
    Unregistered
    Guest
    does the this pointer present in each class (whether you want it or not) thow off the "expected" results by 4-- the size of a pointer?

  11. #11
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    I don't think the "this" pointer would make any sense to be stored in the structure. You have to be accessing the memory already to get it so you would have the address already. my head is spinning, thank you

  12. #12
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,558
    > Salem, how come it doesn't round up when there are just chars?
    The alignment of char is always 1, so there is no need to pack chars to any particular boundary.

    > But when there was a long and an char, sizeof returned 8.
    For you, I'd say the alignment of long is 4
    Since you have 5 bytes already (sizeof(long)+sizeof(char)), the next multiple will be 8

    Consider these two alternatives for arranging a char and a long
    Code:
    #include <iostream.h>
    #include <stddef.h>
    
    struct foo { char a; long b; };
    struct bar { long a; char b; };
    
    int main ( ) {
        cout << offsetof(foo,a) << " " << offsetof(foo,b) << endl;
        cout << offsetof(bar,a) << " " << offsetof(bar,b) << endl;
        return 0;
    }

  13. #13
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    thanks for the info salem.

  14. #14
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    Alignment is processor dependant I believe. If you look in some ms header files you will see

    #pragma pack( 8 )

    So, obviously they are using 8-byte alignment. If you want to see the output as 20 bytes instead of 24, you should use the pragma directive and specify 4 byte alignment. This is really only necessary on palm pilot software and other memory restrictive environments.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Malloc,calloc..Sscanf.
    By ozumsafa in forum C Programming
    Replies: 22
    Last Post: 07-26-2007, 01:09 AM
  2. Replies: 14
    Last Post: 06-28-2006, 01:58 AM
  3. finding size of empty char array
    By darsunt in forum C Programming
    Replies: 12
    Last Post: 05-30-2006, 07:23 PM
  4. Model not showing up on screen...
    By Shamino in forum Game Programming
    Replies: 14
    Last Post: 03-09-2006, 07:00 PM
  5. Need help on pointer to VOID
    By dv007 in forum C Programming
    Replies: 19
    Last Post: 07-09-2002, 06:15 PM

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