473,466 Members | 1,351 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

why is size of object of empty class is 1 byte

Hi all,

An object of a class A which has no member variables and no member
functions, still the size of the object is 1 byte, if there is a byte member
variable then also the size of the object is 1 byte. Could anyone tell me
the reason behind this.

Thanks
ishekara
Jul 22 '05 #1
8 3068

"ishekara" <is******@ishekara.com> wrote in message
Hi all,

An object of a class A which has no member variables and no member
functions, still the size of the object is 1 byte, if there is a byte member variable then also the size of the object is 1 byte. Could anyone tell me
the reason behind this.


http://www.research.att.com/~bs/bs_f...l#sizeof-empty

Sharad
Jul 22 '05 #2
>An object of a class A which has no member variables and no member
functions, still the size of the object is 1 byte, if there is a byte membervariable then also the size of the object is 1 byte. Could anyone tell methe reason behind this.


There is no requirement in the C++ standard that an empty object should
have one byte of memory aoocupied. It is purely based on the
implementation.

Jul 22 '05 #3
ishekara wrote:
Hi all,

An object of a class A which has no member variables and no member
functions, still the size of the object is 1 byte, if there is a byte member
variable then also the size of the object is 1 byte. Could anyone tell me
the reason behind this.

Thanks
ishekara


So that any and all instantiations ( objects ) of the empty version of
class A will have distinct addresses ( evidently at least one byte away
). Thus pointers, references et al of distinct objects will be different.

--

Cheers
--
Hewson::Mike
"This letter is longer than usual because I lack the time to make it
shorter" - Blaise Pascal
Jul 22 '05 #4
Shan wrote:
An object of a class A which has no member variables and no member
functions, still the size of the object is 1 byte, if there is a byte


member
variable then also the size of the object is 1 byte. Could anyone tell


me
the reason behind this.

There is no requirement in the C++ standard that an empty object should
have one byte of memory aoocupied. It is purely based on the
implementation.


Sorry :-)
but, not if it's conforming ( ISO/IEC 14882 p.149 ):

9 Classes [class]
..
..
..
3 Complete objects and member subobjects of class type shall have
nonzero size. ...

--

Cheers
--
Hewson::Mike
"This letter is longer than usual because I lack the time to make it
shorter" - Blaise Pascal
Jul 22 '05 #5
> 3 Complete objects and member subobjects of class type shall have
nonzero size. ...


Sorry, i missed that....And thanks ;))

Jul 22 '05 #6
Shan wrote:
3 Complete objects and member subobjects of class type shall have
nonzero size. ...

Sorry, i missed that....And thanks ;))


Actually I love the way English, as a common language, divides us. :-)

If I say "class X is of size 10 bytes", does that imply that "one byte
of memory occupied". Well yes, it does, but I just didn't mention the
other nine.

--

Cheers
--
Hewson::Mike
"This letter is longer than usual because I lack the time to make it
shorter" - Blaise Pascal
Jul 22 '05 #7
because a objeect's size can not be 0, it is a instance.
but sonetime,a class's size may be 0 when the class will haven't a
instance at anytime.

to read a words what was called "countting object in c++".by meyers.
you will get more things.
good luke

Jul 22 '05 #8

"Shan" <sh*******@rediffmail.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
An object of a class A which has no member variables and no member
functions, still the size of the object is 1 byte, if there is a byte member
variable then also the size of the object is 1 byte. Could anyone tell

me
the reason behind this.


There is no requirement in the C++ standard that an empty object should
have one byte of memory aoocupied.


True, but it does require that it have a nonzero size.
It is purely based on the
implementation.


Yes, the exact nonzero size is implementation-defined.

-Mike
Jul 22 '05 #9

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

Similar topics

2
by: Jéjé | last post by:
Hi everyone. I'm currently implementing a Cache system. Limiting its capacity with the number of object makes no sense, since objects can have any size they want. So I'd like to limit its...
9
by: pembed2003 | last post by:
Hi all, If I have the following: class Foo{ void something(void){;} }; and then later I say: Foo f;
3
by: ThazKool | last post by:
Is there anyway to write a class or struct that has no storage. It only operates on a reference to an already existing type. This is actually an extension to another thread. The thread went a...
3
by: spielmann | last post by:
Hello I want to change the scrollbar size of windows, How can I do that with vb.net I have find this in VB6 but how can we convert simply this code. thx
7
by: Martin Robins | last post by:
I am currently looking to be able to read information from Active Directory into a data warehouse using a C# solution. I have been able to access the active directory, and I have been able to return...
8
by: Dave | last post by:
I am serialising an object to a memory mapped file (using the CreateFileMapping and MapViewOfFile p/invoke calls). These need to know the maximum size of the "file". I can put in a "good guess" ie...
2
by: Harish | last post by:
This was an interview question: The size of an empty class is 1 byte. Why? Should it not be 4 bytes? Because an invisible pointer "this" is part of every class?
7
by: Ajay | last post by:
Hi all, Please tell me why empty class has one Byte Size. thanks in Advance.. Ajay
30
by: ggnguser | last post by:
It's part of a test and I'm stumped. There is a function void foo(char **x) The function signature is given and cannot be changed, so no passing of other values. The test case involves...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
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...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.