473,769 Members | 6,597 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2953
<de*********@gm ail.comwrote in message
news:11******** **************@ d55g2000hsg.goo glegroups.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...@rock etmail.comwrote :
<deepakvs...@gm ail.comwrote in message

news:11******** **************@ d55g2000hsg.goo glegroups.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*********@gm ail.comwrote in message
news:11******** **************@ d55g2000hsg.goo glegroups.com.. .
On Sep 5, 8:51 am, "Jim Langston" <tazmas...@rock etmail.comwrote :
><deepakvs...@g mail.comwrote in message

news:11******* *************** @d55g2000hsg.go oglegroups.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...@rock etmail.comwrote :
<deepakvs...@gm ail.comwrote in message

news:11******** **************@ d55g2000hsg.goo glegroups.com.. .


On Sep 5, 8:51 am, "Jim Langston" <tazmas...@rock etmail.comwrote :
<deepakvs...@gm ail.comwrote in message
>news:11******* *************** @d55g2000hsg.go oglegroups.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...@gm ail.com" <deepakvs...@gm ail.com>
wrote:
On Sep 5, 8:51 am, "Jim Langston" <tazmas...@rock etmail.comwrote :
<deepakvs...@gm ail.comwrote in message
news:11******** **************@ d55g2000hsg.goo glegroups.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 objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Sep 5 '07 #6
<de*********@gm ail.comwrote in message
news:11******** *************@w 3g2000hsg.googl egroups.com...
On Sep 5, 9:43 am, "Jim Langston" <tazmas...@rock etmail.comwrote :
><deepakvs...@g mail.comwrote in message

news:11******* *************** @d55g2000hsg.go oglegroups.com. ..
On Sep 5, 8:51 am, "Jim Langston" <tazmas...@rock etmail.comwrote :
<deepakvs...@g mail.comwrote in message
>>news:11****** *************** *@d55g2000hsg.g ooglegroups.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
3556
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 know it returns two bytes in UniCode. Please help... For ANSI: In ISO/ANSI C++ Standard, 5.3.3 § 1, it stays: "The sizeof operator yields the number of bytes in the object representation of its
1
4175
by: Radde | last post by:
Hi all, class A { }; int main() { A a; cout<<"size "<<sizeof(a)<<endl;
9
8445
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
2184
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
2493
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
2400
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 members, sizeof(some_C_struct) gives u the cumulative size of each of the individual members plus whatever bytes were used for alignment padding
11
2970
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
2809
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
1972
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. The original post at gnu.gcc.help can be found at this link http://groups.google.com/group/gnu.gcc.help/browse_thread/thread/ece55cbbd9c36270/2148a6c1ac6119e1?lnk=st&q=#2148a6c1ac6119e1 Here's the question: class base {
0
9589
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10214
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8872
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7410
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5304
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5447
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3963
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3563
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2815
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.