|
-
September 28th, 2005, 03:26 AM
#1
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
-
September 28th, 2005, 03:44 AM
#2
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 ?
-
September 28th, 2005, 04:15 AM
#3
Re: diffrence in sizeof object
 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
-
September 28th, 2005, 04:35 AM
#4
Re: diffrence in sizeof object
 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 ?
-
September 28th, 2005, 04:38 AM
#5
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
-
September 28th, 2005, 04:41 AM
#6
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
-
September 28th, 2005, 04:51 AM
#7
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
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
-
September 28th, 2005, 04:57 AM
#8
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 ?
-
September 28th, 2005, 05:13 AM
#9
Re: diffrence in sizeof object
 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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|