Question about size of class



 DEVELOP > c-Plus-Plus > Question about size of class

LINK TO THIS PAGE  


rating :  0   |  0


  Page 1 of 1

1

 
Topic: DEVELOP > c-Plus-Plus
User: "Kouisawang"
Date: 28 Oct 2006 01:50:06 AM
Object: 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
.

User: "Alf P. Steinbach"

Title: Re: Question about size of class 28 Oct 2006 01:53:16 AM
* 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.
--
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?
.
User: "Kouisawang"

Title: Re: Question about size of class 28 Oct 2006 02:35:35 AM
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.

--
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?

.
User: "Jim Langston"

Title: Re: Question about size of class 28 Oct 2006 02:38:47 AM
"Kouisawang" <KOuisawang@gmail.com> wrote in message
news:1162020935.153899.228800@i3g2000cwc.googlegroups.com...


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
.

User: "Alf P. Steinbach"

Title: Re: Question about size of class 28 Oct 2006 02:50:32 AM
* 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:

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?
.



User: "Salt_Peter"

Title: Re: Question about size of class 28 Oct 2006 02:42:07 AM
Kouisawang wrote:

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.
.


  Page 1 of 1

1

 


Related Articles
 

NEWER

pg.1232     pg.940     pg.716     pg.544     pg.412     pg.311     pg.234     pg.175     pg.130     pg.96     pg.70     pg.50     pg.35     pg.24     pg.16     pg.10     pg.6     pg.3     pg.1

OLDER