473,386 Members | 1,943 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,386 software developers and data experts.

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 2918
<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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Sunil Menon | last post by:
Dear All, A class having no member variables and only a method sizeof(object) will return 1byte in ANSI and two bytes in Unicode. I have the answer for this of how in works in ANSI. But I don't...
1
by: Radde | last post by:
Hi all, class A { }; int main() { A a; cout<<"size "<<sizeof(a)<<endl;
9
by: Sameer | last post by:
Hi, I have a class. class C1 { int i ; char c ; } ; let me say the size of int is 4 and char is 1. Then if I say sizeof(C1) it should be 5, why is it showing 8 ?
32
by: moleskyca1 | last post by:
This may be stupid question, but why is sizeof(Base) == 1 in: int main(int argc, char* argv) { class Base { }; cout << sizeof(Base) << endl; return 0; }
7
by: Alex Vinokur | last post by:
"Alf P. Steinbach" <alfps@start.nowrote in message news:4k9755Fb3nt6U1@individual.net... struct Empty {}; C: sizeof(Empty) == 0 C++: sizeof(Empty) 0 Why doesn't C need a unique address?
5
by: mathemagic | last post by:
Hello All, I apologise if this has been covered by an earlier post. I just wanted to know how the functions of a class are tied to the class. ie, in C structs, since u can have only data...
11
by: nevergone | last post by:
Hello Everybody In <<Modern C++ Design>Compile-Time Assertions there is : template <boolstruct CompileTimeChecker { CompileTimeChecker(...); }; template <struct CompileTimeChecker<false{ };
40
by: Boltar | last post by:
Hi Why - using gcc on linux - does this return 0 in C but returns 1 in C+ +? I don't get it. #include <stdio.h> struct foo { };
7
by: Yen Kwoon | last post by:
Note: This problem is related to gcc but after some back and forth in group gnu.gcc.help it seems to have morph into more of a c++ specificiation question, hence the transplanting to this group. ...
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: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.