regarding sizeof operator for class with no data variables

P

pratap

consider this code snippet

#include<iostream>
using namespace std;

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

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

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.

Regards
Pratap
 
R

Rolf Magnus

pratap said:
consider this code snippet

#include<iostream>
using namespace std;

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

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

why is the compiler forced to allocate 1 byte of memory for a class
which contains no data members.

Because each object must have a distinct address, but to have an address, it
must actually occupy storage. Think of an array of A. How could each
element have a different address?
suppose i define more than 1 functions inside this class ,why is the size
of the class still 1 byte.

Why not? sizeof(A) actually doesn't give you the size of the class, since
classes don't have a size. It gives you the size of an instance of that
class. Functions don't have a size either.
What is the reason for the allocation of 1 byte by the compiler for the
class A.

It could be any value. The standard just requires it to be at least one
byte.
 

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

No members online now.

Forum statistics

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

Latest Threads

Top