472,951 Members | 1,575 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,951 software developers and data experts.

Question about sizeof a class.

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.

May 10 '06 #1
19 1817
Abubakar wrote:
Hi,
lets say I have the following class:

class AAA{};

if I do:
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
May 10 '06 #2
And why does the standard says that it should be 1?

Ab.

"Carl Daniel [VC++ MVP]" <cp*****************************@mvps.org.nospam >
wrote in message news:uD**************@TK2MSFTNGP03.phx.gbl...
Abubakar wrote:
Hi,
lets say I have the following class:

class AAA{};

if I do:
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

May 10 '06 #3
Abubakar wrote:
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
May 10 '06 #4
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.

May 10 '06 #5
Ok, got it. Thanks all.

Ab.

"Arnaud Debaene" <ad******@club-internet.fr> wrote in message
news:#4**************@TK2MSFTNGP04.phx.gbl...
Abubakar wrote:
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

May 11 '06 #6
>> 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


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.
--
Vladimir Nesterovsky
May 11 '06 #7
Vladimir Nesterovsky wrote:
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
May 12 '06 #8

"Vladimir Nesterovsky" <vl******@nesterovsky-bros.com> a écrit dans le
message de news: um*************@TK2MSFTNGP04.phx.gbl...
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


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.


- 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
May 12 '06 #9
>> 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
May 12 '06 #10
On Fri, 12 May 2006 17:46:49 +0200, "Vladimir Nesterovsky"
<vl******@nesterovsky-bros.com> wrote:
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

--
Doug Harrison
Visual C++ MVP
May 12 '06 #11
>>> 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


This is clever solution! But say, why should I be so smart just not to waste
a space?
--
Vladimir Nesterovsky
May 13 '06 #12
On Sat, 13 May 2006 08:46:37 +0200, "Vladimir Nesterovsky"
<vl******@nesterovsky-bros.com> wrote:
This is clever solution! But say, why should I be so smart just not to waste
a space?


Because empty classes have sizeof == 1. :)

--
Doug Harrison
Visual C++ MVP
May 14 '06 #13
Why do we need empty classes in the first place? Any article or example that
could explain this to me?

Ab.

"Doug Harrison [MVP]" <ds*@mvps.org> wrote in message
news:c8********************************@4ax.com...
On Sat, 13 May 2006 08:46:37 +0200, "Vladimir Nesterovsky"
<vl******@nesterovsky-bros.com> wrote:
This is clever solution! But say, why should I be so smart just not to wastea space?


Because empty classes have sizeof == 1. :)

--
Doug Harrison
Visual C++ MVP

May 15 '06 #14
> 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."

--

Kind regards,
Bruno van Dooren
br**********************@hotmail.com
Remove only "_nos_pam"
May 15 '06 #15
On Mon, 15 May 2006 11:14:12 +0500, "Abubakar" <em**********@yahoo.com>
wrote:
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.)

--
Doug Harrison
Visual C++ MVP
May 15 '06 #16
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.

"Doug Harrison [MVP]" <ds*@mvps.org> wrote in message
news:4v********************************@4ax.com...
On Mon, 15 May 2006 11:14:12 +0500, "Abubakar" <em**********@yahoo.com>
wrote:
Why do we need empty classes in the first place? Any article or example thatcould 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.)

--
Doug Harrison
Visual C++ MVP

May 16 '06 #17

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

May 16 '06 #18
Abubakar wrote:
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
May 16 '06 #19
Thank you all for the book suggestions and the answers.

Regards,

Ab.
"Tamas Demjen" <td*****@yahoo.com> wrote in message
news:O#**************@TK2MSFTNGP05.phx.gbl...
Abubakar wrote:
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

May 17 '06 #20

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Sean W. Quinn | last post by:
Hey folks, I have a question regarding file handling, and the preservation of class structure. I have a class (and I will post snippets of code later in the post) with both primitive data...
3
by: Sunil Menon | last post by:
Dear All, A class having no member variables and only a method sizeof(object) will return 1byte in ANSI and two bytes in Unicode. I have the answer for this of how in works in ANSI. But I don't...
2
by: Xiangliang Meng | last post by:
Hi, all. What will we get from sizeof(a class without data members and virtual functions)? For example: class abnormity { public: string name() { return "abnormity"; }
15
by: natespamacct | last post by:
Hi All, I'm not sure if I'm dealing with a C++ question or a compiler question, so please forgive me if I'm asking in the wrong spot. If so, maybe someone can direct me to more appropriate spot....
17
by: ThazKool | last post by:
Why is it that calling sizeof on a class that contains only one char& returns the number 4? Should it have a 0 size?
1
by: Gena | last post by:
Hi , I'm a newbe to programming and have a small question: I made a small program with a class. in the class's header file I have : double *ptr_output; Void main() { double result;
10
by: thekestrel | last post by:
Why can't I create an instance of A and B in the Class C definition? #include <iostream> class B; class A; class C { public: B b;
2
by: Renji Panicker | last post by:
I have a question regarding references. Consider the following code: <code> #include <iostream> class A { int _a1; int _a2; int _a3; };
11
by: letz | last post by:
Hi, We have a class whose objects are to be allocated in shared memory. To do that a ShmManager base class is defined so that operator new and delete are redefined to allocate segments in shared...
20
by: Plissken.s | last post by:
Is there an efficient way to create a struct of flag in C++? I need to create a struct of boolean flag, like this: struct testStruct { bool flag1; bool flag2; bool flag3; bool flag4; bool...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...

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.