Interview Question
One Question : Even this Empty class will have a Constructor , Destructor, Copy Constructor and Default Overloaded = operator. So no more than 1 byte is required to encompass all this ?
Because the function body is associated with the class itself,
not with the object.
For example:
class Agg
{
int a;
int b;
static int c;
static int d;
void show()
{a=b;}
};
What do you think will be the size of Agg object?
It will be 8. The space for integers a and b.
Everything else (function bodies and static members) is associated
with the class itself, and will not be COPIED into every created object of that class.
It should be at least 1 byte. Say you have a class of no fields init and you are trying to divide something with the size of class object.
If the size is zero, then it will lead in to issues.
For an empty class, compiler creates Constructor, destructor, copy constructor and assignment operator.Hence, the memory in code segment is system dependent (depending on if machine is RISC or CISC) and memory occupied in data segment is 1 byte, for the class to be existing.None i guess on BSS because the memory allocated is initialized.On the stack will grow and reduce depending on if the class is instantiated using default constructor, copy constructor. Hope this helps.
For this I asnwered that, consider that it was zero...
- Crime_master_GoGo August 03, 2010In that case, three objects of this empty class would have had the same address is it not?
while each object's address should be unique. so in order to keep their address unique compiler need to allocate some minimal amount of memory, that's it allocates 1.
but interviewer did not look convinced, any guesses guys