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! 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
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
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
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.
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
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;
}
}
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
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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...
|
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
|
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...
|
by: In a little while |
last post by:
thanks
|
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...
|
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
|
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...
|
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)...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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)...
|
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...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
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...
| |