468,510 Members | 1,590 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,510 developers. It's quick & easy.

sizeof empty class

what is the size of an class with not data members?
ex:
class A {
};

also
sizeof ?
class A{
int f(){
cout<<"Hello";
}
};

Sep 5 '07 #1
6 2648
<de*********@gmail.comwrote in message
news:11**********************@d55g2000hsg.googlegr oups.com...
what is the size of an class with not data members?
ex:
class A {
};

also
sizeof ?
class A{
int f(){
cout<<"Hello";
}
};
Usually 1. Why don't you try it?
Sep 5 '07 #2
On Sep 5, 8:51 am, "Jim Langston" <tazmas...@rocketmail.comwrote:
<deepakvs...@gmail.comwrote in message

news:11**********************@d55g2000hsg.googlegr oups.com...
what is the size of an class with not data members?
ex:
class A {
};
also
sizeof ?
class A{
int f(){
cout<<"Hello";
}
};

Usually 1. Why don't you try it?
i've tried it... but why is it 1? Is it just for memory allocation so
that no two objects are allocated the same space?

Sep 5 '07 #3
<de*********@gmail.comwrote in message
news:11**********************@d55g2000hsg.googlegr oups.com...
On Sep 5, 8:51 am, "Jim Langston" <tazmas...@rocketmail.comwrote:
><deepakvs...@gmail.comwrote in message

news:11**********************@d55g2000hsg.googleg roups.com...
what is the size of an class with not data members?
ex:
class A {
};
also
sizeof ?
class A{
int f(){
cout<<"Hello";
}
};

Usually 1. Why don't you try it?

i've tried it... but why is it 1? Is it just for memory allocation so
that no two objects are allocated the same space?
Yes, among other considerations.
Sep 5 '07 #4
On Sep 5, 9:43 am, "Jim Langston" <tazmas...@rocketmail.comwrote:
<deepakvs...@gmail.comwrote in message

news:11**********************@d55g2000hsg.googlegr oups.com...


On Sep 5, 8:51 am, "Jim Langston" <tazmas...@rocketmail.comwrote:
<deepakvs...@gmail.comwrote in message
>news:11**********************@d55g2000hsg.googleg roups.com...
what is the size of an class with not data members?
ex:
class A {
};
also
sizeof ?
class A{
int f(){
cout<<"Hello";
}
};
Usually 1. Why don't you try it?
i've tried it... but why is it 1? Is it just for memory allocation so
that no two objects are allocated the same space?

Yes, among other considerations.- Hide quoted text -

- Show quoted text -
so you mean to say the actual size of object is 0 but it is allocated
atleast 1 byte..

Sep 5 '07 #5
On Sep 5, 5:54 am, "deepakvs...@gmail.com" <deepakvs...@gmail.com>
wrote:
On Sep 5, 8:51 am, "Jim Langston" <tazmas...@rocketmail.comwrote:
<deepakvs...@gmail.comwrote in message
news:11**********************@d55g2000hsg.googlegr oups.com...
what is the size of an class with not data members?
ex:
class A {
};
also
sizeof ?
class A{
int f(){
cout<<"Hello";
}
};
Usually 1. Why don't you try it?
It's implementation defined. It's usually 1 on byte addressed
machines, but probably the same as sizeof(int) on a word
addressed machine.
i've tried it... but why is it 1? Is it just for memory allocation so
that no two objects are allocated the same space?
It's principally so that no two objects (of the same type) have
the same address. If you write something like:
A array[2] ;
you are guaranteed that &array[0] != &array[1].

Note that there are certain, very special cases where the
compiler is allowed to allocate less memory than the size of the
object. For example, given:

class A {} ;
class B : public A { int i ;} ;

With many compilers, sizeof( B ) == sizeof( int ), even though
sizeof( A ) + sizeof( int ) is larger. (This is called the
"empty base class" optimization.)

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Sep 5 '07 #6
<de*********@gmail.comwrote in message
news:11*********************@w3g2000hsg.googlegrou ps.com...
On Sep 5, 9:43 am, "Jim Langston" <tazmas...@rocketmail.comwrote:
><deepakvs...@gmail.comwrote in message

news:11**********************@d55g2000hsg.googleg roups.com...
On Sep 5, 8:51 am, "Jim Langston" <tazmas...@rocketmail.comwrote:
<deepakvs...@gmail.comwrote in message
>>news:11**********************@d55g2000hsg.google groups.com...
what is the size of an class with not data members?
ex:
class A {
};
also
sizeof ?
class A{
int f(){
cout<<"Hello";
}
};
>Usually 1. Why don't you try it?
i've tried it... but why is it 1? Is it just for memory allocation so
that no two objects are allocated the same space?

Yes, among other considerations
so you mean to say the actual size of object is 0 but it is allocated
atleast 1 byte..
No, the size of the object is 1 byte. The compiler adds a padding byte.
Read James message, an array of 0 sized objects wouldn't work.
Sep 5 '07 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Sunil Menon | last post: by
1 post views Thread by Radde | last post: by
9 posts views Thread by Sameer | last post: by
32 posts views Thread by moleskyca1 | last post: by
11 posts views Thread by nevergone | last post: by
reply views Thread by NPC403 | last post: by
1 post views Thread by fmendoza | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.