473,651 Members | 2,582 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 7211
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<<sizeo f 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 indistinguishab le 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 indistinguishab le 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********@yaho o.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 indistinguishab le 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.l earn.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.********@com Acast.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 indistinguishab le 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
5690
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 difficult to know what is going on. One of these Order Forms you can see here... http://www.cardman.co.uk/orderform.php3
6
4133
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 end is found is less efficient than using sizeof operator , since size of the array is completely determined at compile time. i don't quite understand this. Could anyone explain to me in detail ?
4
16621
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
4921
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 could give some ideas?I am expecting your reply. David.
13
17125
by: In a little while | last post by:
thanks
7
43887
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 object. 4. If string size is less than the size of the rectangle, we are done.
5
2624
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
2938
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 that one line item.i will be able too calculate the value only for the last line item added ,But if i change any of the values in the two boxes from which it takes values.The probablity will not be recalculated. Could anyone help me to fix this? ...
1
1765
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) Handles Button1.Click Dim filelength As Long filelength = GetFileSize("D:\Documents and Settings\Administrator\Desktop\Private Function SaveDetails.doc") MsgBox(filelength) End Sub Private Function GetFileSize(ByVal MyFilePath As String) As Long
0
8347
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8792
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8571
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6157
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5605
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4143
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4280
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2696
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1585
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.