471,107 Members | 1,844 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,107 software developers and data experts.

how C++ calculate the size of an object?

Hi all,
If I have the following:

class Foo{
void something(void){;}
};

and then later I say:

Foo f;

cout<<sizeof f<<endl;

It tells me the size of f is 1 byte. I wonder how does C++ know the
size of f is one byte. Is it because the implicit 'this' pointer?

Thanks!
Jul 22 '05 #1
9 7062
pembed2003 wrote:
Hi all,
If I have the following:

class Foo{
void something(void){;}
};

and then later I say:

Foo f;

cout<<sizeof f<<endl;

It tells me the size of f is 1 byte. I wonder how does C++ know the
size of f is one byte. Is it because the implicit 'this' pointer?

Thanks!


The size of an object is implementation-specific. In your case it appears as
if your implementation padded your class of nothing with one byte,
presumably because an object of 0 bytes cannot have an address.

- Pete
Jul 22 '05 #2
Petec wrote:
pembed2003 wrote:
Hi all,
If I have the following:

class Foo{
void something(void){;}
};

and then later I say:

Foo f;

cout<<sizeof f<<endl;

It tells me the size of f is 1 byte. I wonder how does C++ know the
size of f is one byte. Is it because the implicit 'this' pointer?

Thanks!

The size of an object is implementation-specific. In your case it appears as
if your implementation padded your class of nothing with one byte,
presumably because an object of 0 bytes cannot have an address.


It's not because it cannot have an address, it's because there can be
no array of an objects of size 0, or if there would be, each element
would be indistinguishable from the others.

Base class subobjects can be of size 0, and their address is the same
as the address of the containing object. The only way to figure out
that they have size 0 is to add an empty base to the class and compare
the sizes of an instance of that class before and after.

Victor
Jul 22 '05 #3
Victor Bazarov wrote:
Petec wrote: <snip>


The size of an object is implementation-specific. In your case it
appears as if your implementation padded your class of nothing with
one byte, presumably because an object of 0 bytes cannot have an
address.


It's not because it cannot have an address, it's because there can be
no array of an objects of size 0, or if there would be, each element
would be indistinguishable from the others.


Yes, but how can literally nothing have an address? And AFAIK in C++
non-register objects are required to have an address...

- Pete

Base class subobjects can be of size 0, and their address is the same
as the address of the containing object. The only way to figure out
that they have size 0 is to add an empty base to the class and compare
the sizes of an instance of that class before and after.

Victor


Jul 22 '05 #4
On 8 Jun 2004 13:37:38 -0700 in comp.lang.c++, pe********@yahoo.com
(pembed2003) wrote,
It tells me the size of f is 1 byte. I wonder how does C++ know the
size of f is one byte. Is it because the implicit 'this' pointer?


No, the 'this' pointer points to the object, it isn't part of the
object, and is probably bigger than 1 byte anyway.

The smallest size an object can be is 1 byte because an array of zero
size objects would be all at the same location, and that would cause
problems. It's a rule.
Jul 22 '05 #5
Petec wrote:
Victor Bazarov wrote:
Petec wrote:
<snip>

The size of an object is implementation-specific. In your case it
appears as if your implementation padded your class of nothing with
one byte, presumably because an object of 0 bytes cannot have an
address.


It's not because it cannot have an address, it's because there can be
no array of an objects of size 0, or if there would be, each element
would be indistinguishable from the others.

Yes, but how can literally nothing have an address?


It's not nothing. It's an instance of a class that has no data
members.
And AFAIK in C++
non-register objects are required to have an address...
You K incorrectly. References are not required to have an address
and there is no requirement that they are 'register objects'.

- Pete

Base class subobjects can be of size 0, and their address is the same
as the address of the containing object. The only way to figure out
that they have size 0 is to add an empty base to the class and compare
the sizes of an instance of that class before and after.

Victor


V
Jul 22 '05 #6
This is why an object with no data must have a size of 1.

From the standard
A class with an empty sequence of members and base class objects is an
empty class. Complete objects and member subobjects of an empty class type
shall have nonzero size.
#include <iostream>

class Foo{
void something(void){;}
};

int main()
{
Foo* f1 = new Foo;
Foo* f2 = new Foo;

if (f1 == f2)
{
std::cout << " They are the same" << std::endl;
}
else
{
std::cout << " They are different" << std::endl;
}
}
Jul 22 '05 #7
pembed2003 wrote:
Hi all,
If I have the following:

class Foo{
void something(void){;}
};

and then later I say:

Foo f;

cout<<sizeof f<<endl;

It tells me the size of f is 1 byte. I wonder how does C++ know the
size of f is one byte. Is it because the implicit 'this' pointer?

Thanks!


As a side note, remember that the size of an object
may not be the sum of the size of its members. The
compiler is allowed to add padding after members.
This rule leads to the fact that objects should be
copied and serialized (persisted?) member by member.
--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book

Jul 22 '05 #8
Victor Bazarov <v.********@comAcast.net> wrote in message news:<7w*****************@dfw-read.news.verio.net>...
Petec wrote:
> And AFAIK in C++
non-register objects are required to have an address...


You K incorrectly. References are not required to have an address
and there is no requirement that they are 'register objects'.


Well, references are not objects at all, so Petec is correct.
(8.3.2, especially /3)

Types aren't required to have addresses either, nor do namespaces.
Only objects and functions have addresses, and these address spaces
are logically distinct. Petec is misleadingly incomplete about
objects declared with register; they must have an address as well.
However, it is most wise not to look at it, as that may block the
intended register effect.

Regards,
Michiel Salters
Jul 22 '05 #9
On Tue, 08 Jun 2004 21:11:02 GMT, "Petec" <x@x.x> wrote:
Victor Bazarov wrote:
Petec wrote:

<snip>


The size of an object is implementation-specific. In your case it
appears as if your implementation padded your class of nothing with
one byte, presumably because an object of 0 bytes cannot have an
address.


It's not because it cannot have an address, it's because there can be
no array of an objects of size 0, or if there would be, each element
would be indistinguishable from the others.


Yes, but how can literally nothing have an address? And AFAIK in C++
non-register objects are required to have an address...


int* ptr = new int[0];

ptr is guaranteed to be a unique address, but it can't be
dereferenced.

Tom
--
C++ FAQ: http://www.parashift.com/c++-faq-lite/
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
Jul 22 '05 #10

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by ivan | last post: by
96 posts views Thread by david ullua | last post: by
13 posts views Thread by In a little while | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.