Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > object question

Reply
Thread Tools

object question

 
 
junw2000@gmail.com
Guest
Posts: n/a
 
      07-18-2006
how much memory is allocated when creating an object of an empty class?

Thanks.

Jack

 
Reply With Quote
 
 
 
 
AB
Guest
Posts: n/a
 
      07-18-2006
> 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

 
Reply With Quote
 
 
 
 
mast2as@yahoo.com
Guest
Posts: n/a
 
      07-18-2006

(E-Mail Removed) 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;
}

 
Reply With Quote
 
Alf P. Steinbach
Guest
Posts: n/a
 
      07-18-2006
* http://www.velocityreviews.com/forums/(E-Mail Removed):
> 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?
 
Reply With Quote
 
tarun.mohapatra@gmail.com
Guest
Posts: n/a
 
      07-18-2006

Alf P. Steinbach wrote:
> * (E-Mail Removed):
> > 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)

 
Reply With Quote
 
anubhav.anand@gmail.com
Guest
Posts: n/a
 
      07-18-2006

(E-Mail Removed) wrote:
> Alf P. Steinbach wrote:
> > * (E-Mail Removed):
> > > 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)


 
Reply With Quote
 
Jakob Bieling
Guest
Posts: n/a
 
      07-18-2006
(E-Mail Removed) wrote:

> (E-Mail Removed) 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)


 
Reply With Quote
 
raxitsheth2000@yahoo.co.in
Guest
Posts: n/a
 
      07-18-2006
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

 
Reply With Quote
 
Sumit RAJAN
Guest
Posts: n/a
 
      07-18-2006
(E-Mail Removed) 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 <(E-Mail Removed)>
 
Reply With Quote
 
KMS
Guest
Posts: n/a
 
      07-18-2006
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


(E-Mail Removed) wrote:
> how much memory is allocated when creating an object of an empty class?
>
> Thanks.
>
> Jack


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Dumb question: in documentation, why Object#method, and not Object.method ? Elf M. Sternberg Ruby 15 07-29-2009 01:20 AM
why should base class object points to derived class object (virtualfunction question) ? DanielJohnson C++ 7 01-17-2009 10:15 PM
a basic question about Object, Object& gg9h0st C++ 4 10-24-2006 07:00 AM
Object creation - Do we really need to create a parent for a derieved object - can't the base object just point to an already created base object jon wayne C++ 9 09-22-2005 02:06 AM
Question: Object Reference not set to an instance of an object. =?Utf-8?B?TWFubnkgQ2hvaGFu?= ASP .Net 4 01-03-2005 09:54 PM



Advertisments