472,779 Members | 2,261 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,779 software developers and data experts.

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 3026

"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...
0
by: Rina0 | last post by:
Cybersecurity engineering is a specialized field that focuses on the design, development, and implementation of systems, processes, and technologies that protect against cyber threats and...
0
by: erikbower65 | last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps: 1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal. 2. Connect to...
0
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
0
by: kcodez | last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
0
by: Rina0 | last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
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: lllomh | last post by:
How does React native implement an English player?
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...

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.