473,224 Members | 1,361 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,224 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 2908
<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. ...
1
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
0
by: veera ravala | last post by:
ServiceNow is a powerful cloud-based platform that offers a wide range of services to help organizations manage their workflows, operations, and IT services more efficiently. At its core, ServiceNow...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: mar23 | last post by:
Here's the situation. I have a form called frmDiceInventory with subform called subfrmDice. The subform's control source is linked to a query called qryDiceInventory. I've been trying to pick up the...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
by: jimatqsi | last post by:
The boss wants the word "CONFIDENTIAL" overlaying certain reports. He wants it large, slanted across the page, on every page, very light gray, outlined letters, not block letters. I thought Word Art...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...

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.