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. 19 1869
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
And why does the standard says that it should be 1?
Ab.
"Carl Daniel [VC++ MVP]" <cp************ *************** **@mvps.org.nos pam>
wrote in message news:uD******** ******@TK2MSFTN GP03.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
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(somethin g) >=1
Arnaud
MVP - VC
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.
Ok, got it. Thanks all.
Ab.
"Arnaud Debaene" <ad******@clu b-internet.fr> wrote in message
news:#4******** ******@TK2MSFTN GP04.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(somethin g) >=1
Arnaud MVP - VC
>> 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(somethin g) >=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(somethi ng) >=1" as of heritage.
--
Vladimir Nesterovsky
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(somethi ng) >=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
"Vladimir Nesterovsky" <vl******@neste rovsky-bros.com> a écrit dans le
message de news: um************* @TK2MSFTNGP04.p hx.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(somethin g) >=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(somethi ng) >=1" as of heritage.
Heritage of what? Are you speaking about empty base optimization?
Arnaud
MVP - VC
>> 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(somethi ng) >=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_strin g
implementations have "allocator" instance field, which mostly is an instance
of an empty class, and just wastes the space.
--
Vladimir Nesterovsky This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 structures (ints), and more complex data
structures (strings and vectors) in it, and would like to write the entire
class to a data file that could then
be read back and loaded.
However I'm having difficulty with this -- I found out (due to an...
|
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
know it returns two bytes in UniCode.
Please help...
For ANSI:
In ISO/ANSI C++ Standard, 5.3.3 § 1, it stays: "The sizeof operator
yields the number of bytes in the object representation of its
|
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"; }
|
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.
In migrating from gcc 3.2.3 to gcc 3.4.3 the following situation comes
up.
I have the following:
|
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?
| |
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;
|
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;
|
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;
};
|
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 memory. A typical
class "Foo" then inherit from ShmManager to have get this behaviour.
class ShmManager {
|
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 flag5;
bool flag6;
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
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...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
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...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
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...
| |