473,322 Members | 1,911 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 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 7180
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

53
by: Cardman | last post by:
Greetings, I am trying to solve a problem that has been inflicting my self created Order Forms for a long time, where the problem is that as I cannot reproduce this error myself, then it is...
6
by: Herrcho | last post by:
in K&R Chapter 6.3 it mentions two methods to calculate NKEYS. and points out the first one which is to terminate the list of initializers with a null pointer, then loop along keytab until the...
4
by: ivan | last post by:
Hi, How can we calculate the stack and heap size requried by a C program. Is there any specific formula used? Please suggest. regards, Ivan
96
by: david ullua | last post by:
I am reading "Joel on Software" these days, and am in stuck with the question of "how to calculate bitsize of a byte" which is listed as one of the basic interview questions in Joel's book. Anyone...
13
by: In a little while | last post by:
thanks
7
by: carterweb | last post by:
This is how I do it now. 1. Determine the dimensions of the rectangle. 2. Set a my font size to a fixed maximum size. 3. Apply the font my string and measure the string using the graphics...
5
by: Michael | last post by:
Hi. I need dinamically calculate input text field based on parent static TD before showing content of input. Please, advice. Michael
3
by: coolguyraj | last post by:
I have a javascript code to take value from two text boxes and calculate on triggering the "OnBlur" function and display in the third box. The code works fine with one line item,If i have more...
1
by: mansi sharma | last post by:
Hey Frnds to calculate the File Size everytime we have to give extension such as .txt,.doc. AS FOLL-- Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.