Topic: sizeof empty class
Author: AllanW@my-dejanews.com
Date: 1998/11/11 Raw View
In article <3648CA79.2781@wizard.net>,
James Kuyper <kuyper@wizard.net> wrote:
>
> Christopher M. Gurnee wrote:
> ...
> > In addition to the other posts here, it is useful to note that some
> > (many?) compilers will allow an empty base class to add no additional
> > size to a (non-empty) class derived from it. VC5 is one such compiler.
> > I'm not sure whether or not the standard technically allows this, but I
> > can't think of any reason why not offhand.
>
> Section 1.8 p5 explicitly allows base class sub-objects to have a size
> of 0.
I'm under the impression that this isn't the only case ...
struct Base {
char c;
int i;
};
struct Der : public Base {
char c2;
};
On a 16-bit machine, where alignment of i must be on a 2-byte
boundary, sizeof(Base)==4. But sizeof(Der) could also be 4, if
it put c2 in the padding between c and i... is this right?
--
AllanW@my-dejanews.com is a "Spam Magnet" -- never read.
Please reply in USENET only, sorry.
-----------== Posted via Deja News, The Discussion Network ==----------
http://www.dejanews.com/ Search, Read, Discuss, or Start Your Own
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: AllanW@my-dejanews.com
Date: 1998/11/11 Raw View
In article <slrn74i7nt.6np.sbnaran@localhost.localdomain>,
sbnaran@KILL.uiuc.edu wrote:
>
> On 11 Nov 98 02:49:02 GMT, AllanW@my-dejanews.com
>
> >> My compiler (MSVC50) reports sizeof (Empty) == 1.
>
> >This is required by the standard.
>
> No, the standard requires the sizeof(Empty) > 0
Okay. Is this so completely different?
I suppose that class Empty could have size 65536 without violating
the standard, but such a compiler wouldn't sell very well.
--
AllanW@my-dejanews.com is a "Spam Magnet" -- never read.
Please reply in USENET only, sorry.
-----------== Posted via Deja News, The Discussion Network ==----------
http://www.dejanews.com/ Search, Read, Discuss, or Start Your Own
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: ncm@nospam.cantrip.org (Nathan Myers)
Date: 1998/11/12 Raw View
<AllanW@my-dejanews.com> wrote:
> rado42@my-dejanews.com wrote:
>> Does anybody know what the Standard says about sizeof empty class?
>> E.G.
>> struct Empty { };
>>
>> My compiler (MSVC50) reports sizeof (Empty) == 1.
>
>This is required by the standard.
>
>> Is there any reason that objects of size 0 are not allowed?
>> Bear in mind that the class can have (non virtual) member functions, or just
>> to serve as base class, or whatever. If it does *NOT* have any data members,
>> There is one thing only that member functions might take into account to
>> behave in different way: the value of 'this'. Is this the reason for the
>> limitation 'sizeof whatever > 0' (if it exists)?
>
>You're starting to see some of the reasoning. There are also
>ramifications, for instance:
> Empty e[20];
> int e_size = sizeof(e)/sizeof(e[0]);
>
>However, note that the 1-byte minimum can be optimized away in
>certain situations, for instance:
> struct xxx : public Empty {
> char data[2];
> };
>On many compilers, sizeof(xxx)==2, not 3.
On some compilers, sizeof(xxx)==6 or even 8.
On Egcs/x86 it's 2 if you compile with -fnew-abi, 3 otherwise.
For other platforms it would vary.
For an exploration of the subject, see
http://www.cantrip.org/emptyopt.html
--
Nathan Myers
ncm@nospam.cantrip.org http://www.cantrip.org/
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: AllanW@my-dejanews.com
Date: 1998/11/12 Raw View
In article <3648CA79.2781@wizard.net>,
James Kuyper <kuyper@wizard.net> wrote:
> Section 1.8 p5 explicitly allows base class sub-objects to have a size
> of 0.
5 A conforming implementation executing a well-formed program shall pro-
duce the same observable behavior as one of the possible execution
sequences of the corresponding instance of the abstract machine with
the same program and the same input. However, if any such execution
sequence contains an undefined operation, this International Standard
places no requirement on the implementation executing that program
with that input (not even with regard to operations preceding the
first undefined operation).
Is this the section you meant to quote?
--
AllanW@my-dejanews.com is a "Spam Magnet" -- never read.
Please reply in USENET only, sorry.
-----------== Posted via Deja News, The Discussion Network ==----------
http://www.dejanews.com/ Search, Read, Discuss, or Start Your Own
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: James Kuyper <kuyper@wizard.net>
Date: 1998/11/12 Raw View
AllanW@my-dejanews.com wrote:
>
> In article <3648CA79.2781@wizard.net>,
> James Kuyper <kuyper@wizard.net> wrote:
> > Section 1.8 p5 explicitly allows base class sub-objects to have a size
> > of 0.
>
> 5 A conforming implementation executing a well-formed program shall pro-
> duce the same observable behavior as one of the possible execution
> sequences of the corresponding instance of the abstract machine with
> the same program and the same input. However, if any such execution
> sequence contains an undefined operation, this International Standard
> places no requirement on the implementation executing that program
> with that input (not even with regard to operations preceding the
> first undefined operation).
>
> Is this the section you meant to quote?
Which document were you quoting? I'm talking about ISO/IEC 14882.
Section 1.8, paragraph 5:
| Unless it is a bit-field(9.6), a most derived object shall have a
| non-zero size and shall occupy one or more bytes of storage. Base class
| sub-objects may have zero size. An object of POD 4) type (3.9) shall
| occupy contiguous bytes of storage.
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: Chris Kuan <look@sig.please>
Date: 1998/11/12 Raw View
AllanW@my-dejanews.com wrote:
>
> In article <3648CA79.2781@wizard.net>,
> James Kuyper <kuyper@wizard.net> wrote:
> > Section 1.8 p5 explicitly allows base class sub-objects to have a size
> > of 0.
>
> 5 A conforming implementation executing a well-formed program shall pro-
> duce the same observable behavior as one of the possible execution
> sequences of the corresponding instance of the abstract machine with
> the same program and the same input. However, if any such execution
> sequence contains an undefined operation, this International Standard
> places no requirement on the implementation executing that program
> with that input (not even with regard to operations preceding the
> first undefined operation).
>
> Is this the section you meant to quote?
I doubt it - that's Section 1.9, para. 5 :-)
Looking further up the page:
Unless it is a bit-field (9.6), a most-derived object shall have
a non-zero size and shall occupy one or more bytes of storage.
Base class sub-objects may have zero size. (...)
--
Chris Kuan, BHP Information Technology
Concatenate for email: mr gazpacho @ hotmail . com
Phone : +61 2 4275 5555 Fax : +61 2 4275 5547
"A Design Pattern is something that got left out of the language"
- Richard O'Keefe
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: ncm@nospam.cantrip.org (Nathan Myers)
Date: 1998/11/12 Raw View
<AllanW@my-dejanews.com> wrote:
> James Kuyper <kuyper@wizard.net> wrote:
>> Christopher M. Gurnee wrote:
>> ...
>> > In addition to the other posts here, it is useful to note that some
>> > (many?) compilers will allow an empty base class to add no additional
>> > size to a (non-empty) class derived from it.
>>
>> Section 1.8 p5 explicitly allows base class sub-objects to have a size
>> of 0.
>
>I'm under the impression that this isn't the only case ...
> struct Base {
> char c;
> int i;
> };
> struct Der : public Base {
> char c2;
> };
>On a 16-bit machine, where alignment of i must be on a 2-byte
>boundary, sizeof(Base)==4. But sizeof(Der) could also be 4, if
>it put c2 in the padding between c and i... is this right?
This would not break conformance, but don't expect to find any
that do it. There are ABIs for each architecture that add
requirements on top of what the standards say.
--
Nathan Myers
ncm@nospam.cantrip.org http://www.cantrip.org/
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: rado42@my-dejanews.com
Date: 1998/11/10 Raw View
Hi gurus,
Does anybody know what the Standard says about sizeof empty class?
E.G.
struct Empty { };
My compiler (MSVC50) reports sizeof (Empty) == 1.
Is there any reason that objects of size 0 are not allowed?
Bear in mind that the class can have (non virtual) member functions, or just
to serve as base class, or whatever. If it does *NOT* have any data members,
There is one thing only that member functions might take into account to
behave in different way: the value of 'this'. Is this the reason for the
limitation 'sizeof whatever > 0' (if it exists)?
Radoslav Getov
-----------== Posted via Deja News, The Discussion Network ==----------
http://www.dejanews.com/ Search, Read, Discuss, or Start Your Own
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: Ron Natalie <ron@sensor.com>
Date: 1998/11/10 Raw View
rado42@my-dejanews.com wrote:
>
> Hi gurus,
>
> Does anybody know what the Standard says about sizeof empty class?
> E.G.
> struct Empty { };
>
> My compiler (MSVC50) reports sizeof (Empty) == 1.
>
> Is there any reason that objects of size 0 are not allowed?
The standard says that the size of an object can not be zero.
The primary things this guarantees is that the allocated address
of each object will be unique. In most cases that could be
fixed otherways, but in the case of an array, it can't be.
Empty earray[10];
If earray has size of zero, they all have the same address.
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: James Kuyper <kuyper@wizard.net>
Date: 1998/11/10 Raw View
rado42@my-dejanews.com wrote:
> Hi gurus,
> Does anybody know what the Standard says about sizeof empty class?
> E.G.
> struct Empty { };
> My compiler (MSVC50) reports sizeof (Empty) == 1.
> Is there any reason that objects of size 0 are not allowed?
> Bear in mind that the class can have (non virtual) member functions, or just
> to serve as base class, or whatever. If it does *NOT* have any data members,
> There is one thing only that member functions might take into account to
> behave in different way: the value of 'this'. Is this the reason for the
> limitation 'sizeof whatever > 0' (if it exists)?
Essentially. The key thing is to allow multiple instances of the object
to be created, and have each one have a different address. Otherwise you
couldn't pass them to certain algorithms that think they're working with
ordinary objects. Since the smallest difference between two non-equal
pointers is 1 byte, every object must take up at least 1 byte, even if
it doesn't use that byte for anything.
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: "Christopher M. Gurnee" <gurnec_at_mediaone_dot_net@127.0.0.1>
Date: 1998/11/10 Raw View
rado42@my-dejanews.com wrote in message
<729c8s$9u8$1@nnrp1.dejanews.com>...
>Hi gurus,
>
>Does anybody know what the Standard says about sizeof empty class?
>E.G.
>struct Empty { };
>
>My compiler (MSVC50) reports sizeof (Empty) == 1.
>
>Is there any reason that objects of size 0 are not allowed?
In addition to the other posts here, it is useful to note that some
(many?) compilers will allow an empty base class to add no additional
size to a (non-empty) class derived from it. VC5 is one such compiler.
I'm not sure whether or not the standard technically allows this, but I
can't think of any reason why not offhand.
-Chris Gurnee
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: sbnaran@dirac.ceg.uiuc.edu (Siemel Naran)
Date: 1998/11/10 Raw View
On 10 Nov 1998 21:25:37 GMT, Christopher M. Gurnee
>In addition to the other posts here, it is useful to note that some
>(many?) compilers will allow an empty base class to add no additional
>size to a (non-empty) class derived from it. VC5 is one such compiler.
>I'm not sure whether or not the standard technically allows this, but I
>can't think of any reason why not offhand.
The standard does allow this, and it is called the empty base
optimization. Often, we inherit from classes that just define
typedefs, like std::unary_function. Or we use stateless
classes, such as a std::allocator inside a std::vector.
class vector<T,Alloc> could have a static instance of an Alloc,
but in general, allocators will have state variables, so class
vector<T,Alloc> should contain an Alloc as a contained variable.
Note that a pure virtual base class is not empty -- it has a
virtual pointer.
--
----------------------------------
Siemel B. Naran (sbnaran@uiuc.edu)
----------------------------------
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: James Kuyper <kuyper@wizard.net>
Date: 1998/11/10 Raw View
Christopher M. Gurnee wrote:
...
> In addition to the other posts here, it is useful to note that some
> (many?) compilers will allow an empty base class to add no additional
> size to a (non-empty) class derived from it. VC5 is one such compiler.
> I'm not sure whether or not the standard technically allows this, but I
> can't think of any reason why not offhand.
Section 1.8 p5 explicitly allows base class sub-objects to have a size
of 0.
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: clamage@Eng.Sun.COM (Steve Clamage)
Date: 1998/11/11 Raw View
"Christopher M. Gurnee" <gurnec_at_mediaone_dot_net@127.0.0.1> writes:
>In addition to the other posts here, it is useful to note that some
>(many?) compilers will allow an empty base class to add no additional
>size to a (non-empty) class derived from it. VC5 is one such compiler.
>I'm not sure whether or not the standard technically allows this, but I
>can't think of any reason why not offhand.
Yes, it's allowed. The size constraint in the standard is
deliberately restricted to complete objects, not on subobjects.
Layout of subobjects is up to the implementation.
--
Steve Clamage, stephen.clamage@sun.com
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: AllanW@my-dejanews.com
Date: 1998/11/11 Raw View
In article <729c8s$9u8$1@nnrp1.dejanews.com>,
rado42@my-dejanews.com wrote:
> Hi gurus,
>
> Does anybody know what the Standard says about sizeof empty class?
> E.G.
> struct Empty { };
>
> My compiler (MSVC50) reports sizeof (Empty) == 1.
This is required by the standard.
> Is there any reason that objects of size 0 are not allowed?
> Bear in mind that the class can have (non virtual) member functions, or just
> to serve as base class, or whatever. If it does *NOT* have any data members,
> There is one thing only that member functions might take into account to
> behave in different way: the value of 'this'. Is this the reason for the
> limitation 'sizeof whatever > 0' (if it exists)?
You're starting to see some of the reasoning. There are also
ramifications, for instance:
Empty e[20];
int e_size = sizeof(e)/sizeof(e[0]);
However, note that the 1-byte minimum can be optimized away in
certain situations, for instance:
struct xxx : public Empty {
char data[2];
};
On many compilers, sizeof(xxx)==2, not 3.
--
AllanW@my-dejanews.com is a "Spam Magnet" -- never read.
Please reply in USENET only, sorry.
-----------== Posted via Deja News, The Discussion Network ==----------
http://www.dejanews.com/ Search, Read, Discuss, or Start Your Own
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: sbnaran@localhost.localdomain.COM (Siemel Naran)
Date: 1998/11/11 Raw View
On 11 Nov 98 02:49:02 GMT, AllanW@my-dejanews.com
>> My compiler (MSVC50) reports sizeof (Empty) == 1.
>This is required by the standard.
No, the standard requires the sizeof(Empty) > 0
--
----------------------------------
Siemel B. Naran (sbnaran@uiuc.edu)
----------------------------------
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]