Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / Managed C++ / May 2006

Tip: Looking for answers? Try searching our database.

Question about sizeof a class.

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Abubakar - 10 May 2006 14:24 GMT
Hi,
lets say I have the following class:

class AAA{};

if I do:
sizeof(AAA)
it gives me 1. Why is the size 1 ?

Regards,

Ab.
Carl Daniel [VC++ MVP] - 10 May 2006 14:51 GMT
> Hi,
> lets say I have the following class:
[quoted text clipped - 4 lines]
> sizeof(AAA)
> it gives me 1. Why is the size 1 ?

Because the minimum size of a class is required to be 1 by the C++ standard.

-cd
Abubakar - 10 May 2006 15:22 GMT
And why does the standard says that it should be 1?

Ab.

> > Hi,
> > lets say I have the following class:
[quoted text clipped - 8 lines]
>
> -cd
Arnaud Debaene - 10 May 2006 16:29 GMT
> And why does the standard says that it should be 1?

Because you should always be able to take the address of an object, and the
addresses of 2 distincts objects should always be different. This is
fundamental to be able to write generic code (template or otherwise) that
use pointers. The only way to fullfill those requirements is to always have
sizeof(something) >=1

Arnaud
MVP - VC
Abubakar - 11 May 2006 06:46 GMT
Ok, got it. Thanks all.

Ab.

> > And why does the standard says that it should be 1?
>
[quoted text clipped - 6 lines]
> Arnaud
> MVP - VC
Vladimir Nesterovsky - 11 May 2006 10:31 GMT
>> And why does the standard says that it should be 1?
>
[quoted text clipped - 3 lines]
> use pointers. The only way to fullfill those requirements is to always
> have sizeof(something) >=1

This in not always true taking unioun(s), or object containment, or
threading into account. Two different objects of the union can have same
address, or contained object can have the same address as a container
object.

After all, why should this be so fundamental for the generic code writing
(that uses pointers)? I believe generic code could survive without it.

I'd rather thought of "sizeof(something) >=1" as of heritage.
Signature

Vladimir Nesterovsky

Nikolaos D. Bougalis - 12 May 2006 04:48 GMT
> After all, why should this be so fundamental for the generic code writing
> (that uses pointers)? I believe generic code could survive without it.
> I'd rather thought of "sizeof(something) >=1" as of heritage.

    What is the size of an array of zero-sized classes? How does one go
about accessing the 5th element of an array of such a class? How does
one go about incrementing a pointer to an array of such a class? How
does one compare two pointers to of such a class, to determine if they
are pointing to the same object? What does 'new' when applied to such a
class?

    -n
Arnaud Debaene - 12 May 2006 07:28 GMT
>>> And why does the standard says that it should be 1?
>>
[quoted text clipped - 8 lines]
> address, or contained object can have the same address as a container
> object.

- union is precisely the explicit exception tat allow 2 objects to overlap
(though I believe it is undefined to apply an unin to anything but primitive
types).
- One could argue that in the container/contained case, the contained object
*is* a sub-part of the container, and therefore thay can be seen as being
only one object (I agree this is dubious, especially in case of private
members).
- Concerning threading.... What's your point? My assertion has a meaning
only at one given point in time. Besides, the C++ language as, for now, no
notion of threads so the point is moot from a language definition point of
view.

> After all, why should this be so fundamental for the generic code writing
> (that uses pointers)? I believe generic code could survive without it.
>
> I'd rather thought of "sizeof(something) >=1" as of heritage.
Heritage of what? Are you speaking about empty base optimization?

Arnaud
MVP - VC
Vladimir Nesterovsky - 12 May 2006 16:00 GMT
>> After all, why should this be so fundamental for the generic code writing
>> (that uses pointers)? I believe generic code could survive without it.
>>
>> I'd rather thought of "sizeof(something) >=1" as of heritage.
> Heritage of what?

The issue could be resolved either way, but obviously not now.

> Are you speaking about empty base optimization?

I'm speaking about the fact that many contemporary std:basic_string
implementations have "allocator" instance field, which mostly is an instance
of an empty class, and just wastes the space.
--
Vladimir Nesterovsky
Doug Harrison [MVP] - 13 May 2006 00:43 GMT
>> Are you speaking about empty base optimization?
>
>I'm speaking about the fact that many contemporary std:basic_string
>implementations have "allocator" instance field, which mostly is an instance
>of an empty class, and just wastes the space.

See this article for a way around that problem:

The "Empty Member" C++ Optimization
http://www.cantrip.org/emptyopt.html

Signature

Doug Harrison
Visual C++ MVP

Vladimir Nesterovsky - 13 May 2006 06:50 GMT
>>> Are you speaking about empty base optimization?
>>
[quoted text clipped - 7 lines]
> The "Empty Member" C++ Optimization
> http://www.cantrip.org/emptyopt.html

This is clever solution! But say, why should I be so smart just not to waste
a space?
--
Vladimir Nesterovsky
Doug Harrison [MVP] - 14 May 2006 23:23 GMT
>This is clever solution! But say, why should I be so smart just not to waste
>a space?

Because empty classes have sizeof == 1. :)

Signature

Doug Harrison
Visual C++ MVP

Abubakar - 15 May 2006 07:14 GMT
Why do we need empty classes in the first place? Any article or example that
could explain this to me?

Ab.

> >This is clever solution! But say, why should I be so smart just not to waste
> >a space?
[quoted text clipped - 4 lines]
> Doug Harrison
> Visual C++ MVP
Bruno van Dooren - 15 May 2006 07:39 GMT
> Why do we need empty classes in the first place? Any article or example
> that
> could explain this to me?

from the url supplied by Doug in this thread:
http://www.cantrip.org/emptyopt.html

"They typically declare typedefs or member functions, and you can replace
them with your own classes (which might not be empty) to handle special
needs."

Signature

Kind regards,
   Bruno van Dooren
   bruno_nos_pam_van_dooren@hotmail.com
   Remove only "_nos_pam"

Doug Harrison [MVP] - 15 May 2006 16:25 GMT
>Why do we need empty classes in the first place? Any article or example that
>could explain this to me?

They are useful in a number of contexts:

1. A class can define an interface, but its implementation may be in terms
of another facility defined at a lower level. For example, the class
template std::allocator is defined in terms of the global operators new and
delete. It requires no state of its own, but because it is a class, it can
have data members, and those members may be unique to each instance.

2. It is possible to define class templates that contain nothing but
typedefs that ease (somewhat) the creation of classes that must also define
those typedefs; the latter can derive publicly from the former. An example
would be std::iterator.

3. It is possible to define classes other classes can derive from in order
to indicate type categories. Examples would be std::output_iterator_tag,
std::input_iterator_tag, etc.

4. I once needed a standard way to create a unique type U<T> from a given
type T, because using T directly would interfere with the user's use of a
related facility, and an empty class template provided a way to do that.

5. Classes can be template arguments.

6. Classes have type_info, and map keys can be derived from type_info.

None of the above are possible with namespaces, which are similar to empty
classes that contain nothing but public static members and whose ctors,
dtor, and assignment operator are all private and unimplemented. (Well, I
suppose the first half of (1) can be done with namespaces.)

Signature

Doug Harrison
Visual C++ MVP

Abubakar - 16 May 2006 08:50 GMT
Can u recommend some book(s) that best explains stl, one that is all about
stl? Some 1000 page kind of book. I wanna read all about stl :)

Ab.

> >Why do we need empty classes in the first place? Any article or example that
> >could explain this to me?
[quoted text clipped - 32 lines]
> Doug Harrison
> Visual C++ MVP
adebaene@club-internet.fr - 16 May 2006 13:20 GMT
Abubakar a écrit :

> Can u recommend some book(s) that best explains stl, one that is all about
> stl? Some 1000 page kind of book. I wanna read all about stl :)

"The C++ Standard Library", by Josuttis, is generally viewed as the
reference on the subject. But as all references, it isn't really ideal
as a teaching tool IMHO.

Arnaud
MVP - VC
Tamas Demjen - 16 May 2006 18:37 GMT
> Can u recommend some book(s) that best explains stl, one that is all about
> stl? Some 1000 page kind of book. I wanna read all about stl :)

I recommend Effective STL by Scott Meyers.

Tom
Abubakar - 17 May 2006 07:49 GMT
Thank you all for the book suggestions and the answers.

Regards,

Ab.

> > Can u recommend some book(s) that best explains stl, one that is all about
> > stl? Some 1000 page kind of book. I wanna read all about stl :)
>
> I recommend Effective STL by Scott Meyers.
>
> Tom
Mihajlo Cvetanović - 10 May 2006 16:41 GMT
So that the object can exist, have its place in memory, and be
identified as unique from other objects of the same class.

> And why does the standard says that it should be 1?
>
>>Because the minimum size of a class is required to be 1 by the C++
>>standard.

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.