Question about size of class

K

Kouisawang

Example code:
--------------------------------------------------------------------------------
class Foo{
private:
double a;
int b;
};
--------------------------------------------------------------------------------

Question here is that when member variable is of type int (require 4
bytes)
and double (require 8 bytes), why the total size is 16, not 12?

I used "sizeof" to check the size.

Thanks in advance
-O.Kittipot
 
A

Alf P. Steinbach

* Kouisawang:
Example code:
--------------------------------------------------------------------------------
class Foo{
private:
double a;
int b;
};
--------------------------------------------------------------------------------

Question here is that when member variable is of type int (require 4
bytes)
and double (require 8 bytes), why the total size is 16, not 12?

You mean, "require ... with my compiler".

And the answer is, your compiler, with the options you have specified,
adds some unused space, e.g. for making access more efficient or (on
some computers) possible.
 
K

Kouisawang

Alf said:
* Kouisawang:

You mean, "require ... with my compiler".

Could you clarify about the options that I have specified? What is it?
Can I change its size?
 
J

Jim Langston

Kouisawang said:
Could you clarify about the options that I have specified? What is it?
Can I change its size?

Some compilers let you change the alignment of variables. Sometimes,
however, when you do that the code doesn't work anymore on some specific
machines. I would suggest wholeheartedly not to do this, but if you're
using a Microsoft comopiler, look at #pragma pack
 
S

Salt_Peter

Kouisawang said:
Example code:
--------------------------------------------------------------------------------
class Foo{
private:
double a;
int b;
};
--------------------------------------------------------------------------------

Question here is that when member variable is of type int (require 4
bytes)
and double (require 8 bytes), why the total size is 16, not 12?

I used "sizeof" to check the size.

Thanks in advance
-O.Kittipot

Padding is added according to the platform in use. Otherwise, if you
had a huge container of these, your system would slow to a crawl. Don't
worry about it.
 
A

Alf P. Steinbach

* Kouisawang:
[Placing response above relevant quote]
[Quoting signature]

Please don't top-post, /especially/ not in the middle of an article!

And please don't quote signatures.

Thank you.


* Kouisawang:
Could you clarify about the options that I have specified? What is it?
Can I change its size?

Nobody but you know which options you have specified, or even which
compiler you're using.

Thus, nobody but you know which options are available to you.

However, you might look at alignment options. There might also be some
option to specify the sizes of 'double' and 'int'. E.g., compiling for
different environments, or some compatibility mode, or whatever.

But *DO NOT* ask about compiler specific options in this group.

Ask in the/an appropriate group for your compiler.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top