473,385 Members | 1,356 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

object question

how much memory is allocated when creating an object of an empty class?

Thanks.

Jack

Jul 18 '06 #1
19 1543
AB
how much memory is allocated when creating an object of an empty class?

Try this...

class Empty
{
} ;

Empty e ;

cout<<"\nSizeof "<<typeid(e).name()<<" = "<<sizeof(Empty) ;

Output is 1 when using MSVC 8.0

Jul 18 '06 #2

junw2...@gmail.com wrote:
how much memory is allocated when creating an object of an empty class?

Thanks.
I imaging that this would be a good way of getting the size of a
class/instance of a class -

class DoNothing
{
public:
DoNothing() {}
//int a;
};

int main()
{
DoNothing donothing;
std::cout << sizeof( donothing ) << std::endl;
std::cout << sizeof( DoNothing ) << std::endl;
}

Jul 18 '06 #3
* ju******@gmail.com:
how much memory is allocated when creating an object of an empty class?
Depends on the compiler and the context.

As a base class object it can occupy zero bytes, otherwise more.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jul 18 '06 #4

Alf P. Steinbach wrote:
* ju******@gmail.com:
how much memory is allocated when creating an object of an empty class?
It purely depends upon the compiler. But in most of the compilers it
will be 1 byte(Size of char). Emplty class object takes the minimum
space to identify it as an entity in memory.

If it will not take any space the in the below example
Class A{
};

A a, b;

&a and &b can be same. Which is pratically impossible(two
differentities can't reside in same place)

Jul 18 '06 #5

ta*************@gmail.com wrote:
Alf P. Steinbach wrote:
* ju******@gmail.com:
how much memory is allocated when creating an object of an empty class?
>
It purely depends upon the compiler. But in most of the compilers it
will be 1 byte(Size of char). Emplty class object takes the minimum
space to identify it as an entity in memory.

If it will not take any space the in the below example
Class A{
};

A a, b;
Here you are not talking about size of a and b, &a and &b means
"address of".
&a and &b will not be same in above case, but this is nothing to do
with size
Size of a and b will be same but they will have different address. But
yes, size depends upon compliers and usually 1 byte.
&a and &b can be same. Which is pratically impossible(two
differentities can't reside in same place)
Jul 18 '06 #6
an***********@gmail.com wrote:
ta*************@gmail.com wrote:
>Class A{
};

A a, b;
>&a and &b can be same. Which is pratically impossible(two
differentities can't reside in same place)
Here you are not talking about size of a and b, &a and &b means
"address of".
&a and &b will not be same in above case, but this is nothing to do
with size
Size of a and b will be same but they will have different address. But
yes, size depends upon compliers and usually 1 byte.
You misunderstood him. If 'a' and 'b' *would* have a size of 0 *and*
they are allocated right next to each other, so that a + sizeof(a) == b,
then &a == &b, because sizeof(a) == 0. Since &a == &b is invalid for
different objects, sizeof(a) cannot be 0 in this case.

So in the above example, &a and &b (address of 'a' and address of
'b') could be the same, if sizeof(a) could be 0, which is what tarun
said.

regards
--
jb

(reply address in rot13, unscramble first)
Jul 18 '06 #7
Replying to Alf P. Steinbach
I dont't think logically Object of size 0 is possible.

Even for emptyclass it would required some min. byte (which is on most
platform 1 byte,)
--
Raxit Sheth

Jul 18 '06 #8
ra************@yahoo.co.in wrote:
Replying to Alf P. Steinbach
Please quote what you are replying to.
I dont't think logically Object of size 0 is possible.

Even for emptyclass it would required some min. byte (which is on most
platform 1 byte,)
If you look carefully at Alf's reply, you will find that he says it
*can* be zero in case of a *base* class. Otherwise, the only requirement
is that it have a non-zero size.

Google for "Empty Base Class Optimization" in this regard.
--
Sumit RAJAN <su*********@geoconcept.com>
Jul 18 '06 #9
KMS
Hi,

It depends on compiler and it will take 1 byte in most of the
compilers.

Reason:

Class can be empty but there are possibilities that there can be an
empty class with virtual function, in which case there will be a
virtual table and it need a pointer to store address of virtual
table.So we need a byte to store the address of virtual table.

I remember that'z what even Bjarne Stroustroup said in his book C++
Programming Language.

Thanks
Suresh K.M
ju******@gmail.com wrote:
how much memory is allocated when creating an object of an empty class?

Thanks.

Jack
Jul 18 '06 #10
Jakob Bieling wrote:
an***********@gmail.com wrote:
>ta*************@gmail.com wrote:
>>Class A{
};

A a, b;
>>&a and &b can be same. Which is pratically impossible(two
differentities can't reside in same place)
>Here you are not talking about size of a and b, &a and &b means
"address of".
&a and &b will not be same in above case, but this is nothing to do
with size
Size of a and b will be same but they will have different address. But
yes, size depends upon compliers and usually 1 byte.

You misunderstood him. If 'a' and 'b' *would* have a size of 0 *and*
they are allocated right next to each other, so that a + sizeof(a) == b,
then &a == &b, because sizeof(a) == 0. Since &a == &b is invalid for
different objects, sizeof(a) cannot be 0 in this case.
Nobody forces the compiler to put a and b directly next to each other. So
that's not really a good example.

The example of an array is better. In:

A array[5];

each array member must have a distinct address, but that's not possible if
the members have a size of zero. All elements would be at the same address,
or depending on your view, actually not be at any address at all. After
all, an address marks some storage location, and an object with a size of
zero does not occupy any storage and thus can't have a location there.
Jul 18 '06 #11
* ta*************@gmail.com:
Alf P. Steinbach wrote:
>* ju******@gmail.com:
>>how much memory is allocated when creating an object of an empty class?
It purely depends upon the compiler.
No, I'm sorry, that's incorrect.

As I wrote (but you snipped all I wrote, don't do that) it depends on
the compiler but also on the context.

Quoting myself:

<q>
Depends on the compiler and the context.

As a base class object it can occupy zero bytes, otherwise more.
</q>

It purely depends upon the compiler. But in most of the compilers it
will be 1 byte(Size of char). Emplty class object takes the minimum
space to identify it as an entity in memory.

If it will not take any space the in the below example
Class A{
};

A a, b;

&a and &b can be same. Which is pratically impossible(two
differentities can't reside in same place)
Right.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jul 18 '06 #12
ju******@gmail.com wrote:
how much memory is allocated when creating an object of an empty class?
All objects must occupy at least one byte.
Your compiler is free to generate anything that size or
greater.
Jul 18 '06 #13
an***********@gmail.com wrote:
ta*************@gmail.com wrote:
>Alf P. Steinbach wrote:
>>* ju******@gmail.com:
how much memory is allocated when creating an object of an empty class?
It purely depends upon the compiler. But in most of the compilers it
will be 1 byte(Size of char). Emplty class object takes the minimum
space to identify it as an entity in memory.

If it will not take any space the in the below example
Class A{
};

A a, b;

Here you are not talking about size of a and b, &a and &b means
"address of".
&a and &b will not be same in above case, but this is nothing to do
with size
Size of a and b will be same but they will have different address. But
yes, size depends upon compliers and usually 1 byte.
A better example would be
A array[2];

The size of an element in the array can't be zero or else
the array indexing rules wouldn't work.
Jul 18 '06 #14
posted:
how much memory is allocated when creating an object of an empty class?

Here's a link to Bjarne's answer:
http://www.research.att.com/~bs/bs_f...l#sizeof-empty
--

Frederick Gotham
Jul 18 '06 #15
Rolf Magnus <ra******@t-online.dewrote:
Jakob Bieling wrote:
>an***********@gmail.com wrote:
>>ta*************@gmail.com wrote:
>>>Class A{
};

A a, b;
>>>&a and &b can be same. Which is pratically impossible(two
differentities can't reside in same place)
>>Here you are not talking about size of a and b, &a and &b means
"address of".
&a and &b will not be same in above case, but this is nothing to do
with size
Size of a and b will be same but they will have different address.
But yes, size depends upon compliers and usually 1 byte.

You misunderstood him. If 'a' and 'b' *would* have a size of 0
*and* they are allocated right next to each other, so that a +
sizeof(a) == b, then &a == &b, because sizeof(a) == 0. Since &a ==
&b is invalid for different objects, sizeof(a) cannot be 0 in this
case.

Nobody forces the compiler to put a and b directly next to each
other. So that's not really a good example.
Right, assume tarun was referring to the possibility of this event.
I should have made clearer that the "*and* they are allocated right next
to each other" also belongs to the "if this happens" part.

regards
--
jb

(reply address in rot13, unscramble first)
Jul 18 '06 #16
ra************@yahoo.co.in wrote:
Replying to Alf P. Steinbach
When you reply, use the standard quoting mechanism. Google now provides
that by default. See the FAQ for other tips:
<http://www.parashift.com/c++-faq-lite/how-to-post.html#faq-5.4>

Brian
Jul 18 '06 #17
KMS wrote:
Hi,

Please don't top-post. Your reply belongs following or interspersed
with properly trimmed quotes. See the newsgroup FAQ:
<http://www.parashift.com/c++-faq-lite/how-to-post.html#faq-5.4>


Brian
Jul 18 '06 #18
Jakob Bieling <ar****************@rot13.comwrote:
Right, assume tarun was referring to the possibility of this event.
*I assumed
--
jb

(reply address in rot13, unscramble first)
Jul 18 '06 #19

Frederick Gotham wrote:
posted:
how much memory is allocated when creating an object of an empty class?


Here's a link to Bjarne's answer:
http://www.research.att.com/~bs/bs_f...l#sizeof-empty
Thank you all.
For the followinf code:

#include <iostream>
class A{

};

int main(){
A a[5];
std::cout<<"sizeof(a):"<<sizeof(a)<<std::endl;
}

THe output is:

sizeof(a):5

I use gcc.

Jul 18 '06 #20

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

Similar topics

1
by: sunaina | last post by:
This is the first program I am writing using PHP and Mysql. I am creating a game where user thinks of an object and my program guesses the object while asking series of yes/no questions. All a...
6
by: Chris S. | last post by:
I'm trying to make a graphical editor and browser for Pickled files. One aspect I'm not sure about is how to detect multiple references to the same data. For instance, say I had the Pickled...
4
by: Tom | last post by:
I want to open a recordset object on an .asp page. When I open the recordset I would like to use a stored procedure that expects a parameter to be passed for the stored procedure. I will then use...
4
by: Mark D. Anderson | last post by:
About a month ago Richard Cornford did an interesting analysis of a memory leak in jscript (internet explorer) when there are "circular" references between DOM objects and (real) jscript objects:...
6
by: Luke | last post by:
Here is my emails to Danny Goodman (but probably he is very busy so he didn't answered it). First email(simple): Subject: JavaScript Arrays " We all know the array can act like HashMap, but is...
100
by: E. Robert Tisdale | last post by:
What is an object? Where did this term come from? Does it have any relation to the objects in "object oriented programming"?
6
by: Tom | last post by:
I have a problem, to which I have been unable to find a solution for days now, after checking numerous references (both in books and online). Perhaps someone here can help. Here's my problem: ...
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...
26
by: yb | last post by:
Hi, Is there a standard for the global 'window' object in browsers? For example, it supports methods such as setInterval and clearInterval, and several others. I know that w3c standardized...
15
by: Hamed | last post by:
Have I posted the message to wrong newsgroup? Or Does the question is so much strage? Would someone please kindly direct me to a true newsgroup or resource? Best Regards Hamed
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...

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.