how C++ calculate the size of an object?

P

pembed2003

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!
 
P

Petec

pembed2003 said:
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
 
V

Victor Bazarov

Petec said:
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
 
P

Petec

Victor said:
Petec wrote:

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
 
D

David Harmon

On 8 Jun 2004 13:37:38 -0700 in comp.lang.c++, (e-mail address removed)
(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.
 
V

Victor Bazarov

Petec said:
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'.

V
 
M

Michael D. Borghardt

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;
}
}
 
T

Thomas Matthews

pembed2003 said:
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
 
M

Michiel Salters

Victor Bazarov said:
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
 
T

tom_usenet

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
 

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

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,902
Latest member
Elena68X5

Latest Threads

Top