LeelaSwamy
asked on
Empty Class
The following code:
Class Fred
{
};
main()
{
Fred f;
cout<<sizeof( f );
}
Result : 1
How?
Class Fred
{
};
main()
{
Fred f;
cout<<sizeof( f );
}
Result : 1
How?
The C++ standard states that the minimum size for any class that can be instantiated (i.e. non-abstract class) is 1 byte, so compilers allocate 1 byte of space for it.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
You can find "Empty Classes" document in MSDN.
Some part mentioning here...
You can declare empty classes, but objects of such types still have nonzero size. The following example illustrates this:
#include <iostream.h>
class NoMembers
{
};
void main()
{
NoMembers n; // Object of type NoMembers.
cout << "The size of an object of empty class is: "
<< sizeof n << endl;
}
This is the output of the preceding program:
The size of an object of empty class is: 1.
The memory allocated for such objects is of nonzero size; therefore, the objects have different addresses. Having different addresses makes it possible to compare pointers to objects for identity. Also, in arrays, each member array must have a distinct address.
Microsoft Specific
An empty base class typically contributes zero bytes to the size of a derived class.
END Microsoft Specific
////////////////////////// ////////// ////////// ////
Also find a document named "sizeof Operator" that also giving specification about that.
GOOD LUCK
Some part mentioning here...
You can declare empty classes, but objects of such types still have nonzero size. The following example illustrates this:
#include <iostream.h>
class NoMembers
{
};
void main()
{
NoMembers n; // Object of type NoMembers.
cout << "The size of an object of empty class is: "
<< sizeof n << endl;
}
This is the output of the preceding program:
The size of an object of empty class is: 1.
The memory allocated for such objects is of nonzero size; therefore, the objects have different addresses. Having different addresses makes it possible to compare pointers to objects for identity. Also, in arrays, each member array must have a distinct address.
Microsoft Specific
An empty base class typically contributes zero bytes to the size of a derived class.
END Microsoft Specific
//////////////////////////
Also find a document named "sizeof Operator" that also giving specification about that.
GOOD LUCK
As a final note, the main reason for this is that no two objects can share the same
address (from the C++ standard). This forces all objects to have a non-zero size
so that you can allocate an array of empty objects and each one has a unique
address. It's just an implementation reality and an edge case in the standard.
address (from the C++ standard). This forces all objects to have a non-zero size
so that you can allocate an array of empty objects and each one has a unique
address. It's just an implementation reality and an edge case in the standard.
LeelaSwamy,
You have posted a total of five questions, and all five questions are open, and have not been awarded points.
You have 5 questions open in the C++ topic area, some of which are two months old.
Please award points for your previous questions and closed them.
You have posted a total of five questions, and all five questions are open, and have not been awarded points.
You have 5 questions open in the C++ topic area, some of which are two months old.
Please award points for your previous questions and closed them.
No comment has been added lately, so it's time to clean up this TA.
I will leave the following recommendation for this question in the Cleanup topic area:
Accept: mnashadka {http:#9239709}
Please leave any comments here within the next seven days.
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!
Tinchos
EE Cleanup Volunteer
I will leave the following recommendation for this question in the Cleanup topic area:
Accept: mnashadka {http:#9239709}
Please leave any comments here within the next seven days.
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!
Tinchos
EE Cleanup Volunteer