regarding sizeof operator for class with no data variables

P

pratap

consider this code snippet

why is the compiler forced to allocate 1 byte of memory for a class
which contains no data members.suppose i define more than 1 functions
inside this class ,why is the size of the class still 1 byte. What is
the reason for the allocation of 1 byte by the compiler for the class
A.

#include<iostream>
using namespace std;

class A
{
void func()
{ }
void func1()
{ }
void func2()
{ }
void func3()
{ }
};

int main()
{
cout<<sizeof(A)<<endl;
return 0;
}


Regards
Pratap
 
?

=?ISO-8859-1?Q?Erik_Wikstr=F6m?=

consider this code snippet

why is the compiler forced to allocate 1 byte of memory for a class
which contains no data members.suppose i define more than 1 functions
inside this class ,why is the size of the class still 1 byte. What is
the reason for the allocation of 1 byte by the compiler for the class
A.

The class does not have a size, an object of the class does. And the
reason is that each object must have an unique address, and how you you
have an unique address if you don't have a size? And the reason they
need an address is because that's how you refer to an object (in the end
when all fancy variable names have been removed by the compiler).
 
G

Guest

I think you should read "Inside the C++ Object Model" written by
Stanley B. Lippman, you will get what you want there.
 
?

=?ISO-8859-1?Q?Erik_Wikstr=F6m?=

I think you should read "Inside the C++ Object Model" written by
Stanley B. Lippman, you will get what you want there.

Please quote the text you are replying to, if you don't it's hard to
determine your intentions. As an example, since you replied to my reply
to the op, your reply might be taken to mean that I was wrong and you
are correcting me.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top