CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    Sep 2005
    Location
    New Delhi, India
    Posts
    332

    diffrence in sizeof object

    I am using VC 6.0

    Code:
    class C
     { 
            char c; 
            int int1; 
            int int2; 
            int i; 
            long l; 
            short s; 
    }; 
    The size of this class is 24 bytes
    
    class C { 
            int int1; 
            int int2; 
            int i; 
            long l; 
            short s; 
            char c; 
    }; 
    Now the size of this class is 20 bytes.
    Why is it happening
    Appreciate others by rating good posts

    "Only buy something that you'd be perfectly happy to hold if the market shut down for 10 years." - Warren Buffett

  2. #2
    Join Date
    Dec 2004
    Location
    Poland
    Posts
    1,165

    Re: diffrence in sizeof object

    The cause of this is 'structure padding'. This option can be changed at compiler options dialog window. Search the forums or google for 'structure padding' or 'srtucture alignment'

    Hob
    B+!
    'There is no cat' - A. Einstein

    Use [code] [/code] tags!

    Did YOU share your photo with us at CG Members photo gallery ?

  3. #3
    Join Date
    Sep 2005
    Location
    New Delhi, India
    Posts
    332

    Re: diffrence in sizeof object

    Quote Originally Posted by Hobson
    The cause of this is 'structure padding'. This option can be changed at compiler options dialog window. Search the forums or google for 'structure padding' or 'srtucture alignment'

    Hob
    thanx i searched on the net(as you said)....and did found out the reason for it...but didn't found anything on switching off this option in VC 6.0
    Appreciate others by rating good posts

    "Only buy something that you'd be perfectly happy to hold if the market shut down for 10 years." - Warren Buffett

  4. #4
    Join Date
    Dec 2004
    Location
    Poland
    Posts
    1,165

    Re: diffrence in sizeof object

    Quote Originally Posted by sunnypalsingh
    thanx i searched on the net(as you said)....and did found out the reason for it...but didn't found anything on switching off this option in VC 6.0
    Go to 'Project settings..' dialog, choose 'C / C++' tab, and click on combobox where 'General' is placed. Choose 'Code generation' option in that combo, and option 'Struct member alignment' will become visible.

    Same effect can be achieved with "#pragma pack' directive.

    Anyway, is it necessary for you to change this setting?

    Hob
    B+!
    'There is no cat' - A. Einstein

    Use [code] [/code] tags!

    Did YOU share your photo with us at CG Members photo gallery ?

  5. #5
    Join Date
    Sep 2005
    Location
    New Delhi, India
    Posts
    332

    Re: diffrence in sizeof object

    No it wasn't necessary....just curious to know more about compiler settings
    Appreciate others by rating good posts

    "Only buy something that you'd be perfectly happy to hold if the market shut down for 10 years." - Warren Buffett

  6. #6
    Join Date
    Dec 2002
    Location
    St.Louis MO, USA
    Posts
    672

    Re: diffrence in sizeof object

    If u use Pragma Pack(1) it reduced to 19 only
    Code:
    #pragma pack(1)
    class Cq
    { 
    	char c; 
     	int int1; 
    	int int2; 
    	int i; 
    	long l; 
    	short s;
    	
    	
    };
    For further details Take a LOOK at : C/C++ Language Reference PACK and /Zp compiler option

    MSDN :The default data member packing for structures and classes is 8 bytes. MFC headers change this to 4 bytes to save memory. Because afxdocob.h is missing the #pragma pack(pop), all user class declarations compiled after including this file will be packed with 4-byte alignment.
    Last edited by ABDULNAUMAN; September 28th, 2005 at 04:43 AM.
    A Person who is polite is given goodness and a person who is away from Politeness is away from Goodness.

    NAUMAAN

  7. #7
    Join Date
    Sep 2005
    Location
    New Delhi, India
    Posts
    332

    Re: diffrence in sizeof object

    It didn't work with project..setting..etc when i changes it to 1 byte alignmentand it gave me the warning
    warning C4653: compiler option 'structure packing (/Zp)' inconsistent with precompiled header; current command-line option ignored

    but it did work with the pragma option
    Code:
    #pragma pack(1)
    Appreciate others by rating good posts

    "Only buy something that you'd be perfectly happy to hold if the market shut down for 10 years." - Warren Buffett

  8. #8
    Join Date
    Dec 2004
    Location
    Poland
    Posts
    1,165

    Re: diffrence in sizeof object

    Maybe 'Rebuild All' would help. Give it a try.
    B+!
    'There is no cat' - A. Einstein

    Use [code] [/code] tags!

    Did YOU share your photo with us at CG Members photo gallery ?

  9. #9
    Join Date
    Sep 2005
    Location
    New Delhi, India
    Posts
    332

    Re: diffrence in sizeof object

    Quote Originally Posted by Hobson
    Maybe 'Rebuild All' would help. Give it a try.
    Nice...it did worked
    Appreciate others by rating good posts

    "Only buy something that you'd be perfectly happy to hold if the market shut down for 10 years." - Warren Buffett

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured