 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Kouisawang Guest
|
Posted: Sat Oct 28, 2006 9:10 am Post subject: Question about size of class |
|
|
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 |
|
| Back to top |
|
 |
Salt_Peter Guest
|
Posted: Sat Oct 28, 2006 9:10 am Post subject: Re: Question about size of class |
|
|
Kouisawang wrote:
| Quote: | 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. |
|
| Back to top |
|
 |
Alf P. Steinbach Guest
|
Posted: Sat Oct 28, 2006 9:10 am Post subject: Re: Question about size of class |
|
|
* 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:
| Quote: | Alf P. Steinbach wrote:
* 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".
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.
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail? |
|
| Back to top |
|
 |
Jim Langston Guest
|
Posted: Sat Oct 28, 2006 9:10 am Post subject: Re: Question about size of class |
|
|
"Kouisawang" <KOuisawang (AT) gmail (DOT) com> wrote in message
news:1162020935.153899.228800 (AT) i3g2000cwc (DOT) googlegroups.com...
| Quote: |
Alf P. Steinbach wrote:
* 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".
Could you clarify about the options that I have specified? What is it?
Can I change its size?
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.
|
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 |
|
| Back to top |
|
 |
Alf P. Steinbach Guest
|
Posted: Sat Oct 28, 2006 9:10 am Post subject: Re: Question about size of class |
|
|
* Kouisawang:
| Quote: | 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.
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail? |
|
| Back to top |
|
 |
Kouisawang Guest
|
Posted: Sat Oct 28, 2006 9:10 am Post subject: Re: Question about size of class |
|
|
Alf P. Steinbach wrote:
| Quote: | * 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".
|
Could you clarify about the options that I have specified? What is it?
Can I change its size?
| Quote: | 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.
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail? |
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|