Click to See Complete Forum and Search --> : diffrence in sizeof object
sunnypalsingh
September 28th, 2005, 03:26 AM
I am using VC 6.0
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
Hobson
September 28th, 2005, 03:44 AM
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
sunnypalsingh
September 28th, 2005, 04:15 AM
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
Hobson
September 28th, 2005, 04:35 AM
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
sunnypalsingh
September 28th, 2005, 04:38 AM
No it wasn't necessary....just curious to know more about compiler settings
Naumaan
September 28th, 2005, 04:41 AM
If u use Pragma Pack(1) it reduced to 19 only
#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 (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vccelng/htm/pragm_22.asp)
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.
sunnypalsingh
September 28th, 2005, 04:51 AM
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
#pragma pack(1)
Hobson
September 28th, 2005, 04:57 AM
Maybe 'Rebuild All' would help. Give it a try.
sunnypalsingh
September 28th, 2005, 05:13 AM
Maybe 'Rebuild All' would help. Give it a try.
Nice...it did worked
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.