C++: What Is the Size of an Object of an Empty Class

Why is the size of an empty class in C++ not zero?

The standard does not allow objects (and classes thereof) of size 0, since that would make it possible for two distinct objects to have the same memory address. That's why even empty classes must have a size of (at least) 1.

C++: What is the size of an object of an empty class?

Quoting Bjarne Stroustrup's C++ Style and Technique FAQ, the reason the size is non-zero is "To ensure that the addresses of two different objects will be different." And the size can be 1 because alignment doesn't matter here, as there is nothing to actually look at.

what is the size of empty class in C++,java?

Short Answer for C++:

The C++ standard explicitly says that a class can not have zero size.

Long Answer for C++:

Because each object needs to have a unique address (also defined in the standard) you can't really have zero sized objects.

Imagine an array of zero sized objects. Because they have zero size they would all line up on the same address location. So it is easier to say that objects can not have zero size.

Note:

Even though an object has a non zero size, if it actually takes up zero room it does not need to increase the size of derived class:

Example:

#include <iostream>

class A {};
class B {};
class C: public A, B {};

int main()
{
std::cout << sizeof(A) << "\n";
std::cout << sizeof(B) << "\n";
std::cout << sizeof(C) << "\n"; // Result is not 3 as intuitively expected.
}

g++ ty.cpp
./a.out
1
1
1

Why the size of empty class that is derived from two empty classes is 2?

To me it seems that whether or not the empty base optimization can be applied here, depends on how one interprets [intro.object/8]:

Unless an object is a bit-field or a base class subobject of zero
size, the address of that object is the address of the first byte it
occupies. Two objects a and b with overlapping lifetimes that are not
bit-fields may have the same address if one is nested within the
other, or if at least one is a base class subobject of zero size and
they are of different types
; otherwise, they have distinct addresses.

Are B and C different types? They both are an A as well. Two distinct A objects actually. A compiler writer is allowed to stop right there an allocate storage for B and C separately, without checking that A is empty.

It's worth noting that with g++ the size is back to 1 if you have B and C inherit from separate bases:

Live Example

#include<iostream>

class A {};
class A1 {};
class B : public A {};
class C : public A1 {};
class D : public B, public C {};

int main() {
std::cout << "Size of D is: " << sizeof(D) << std::endl;
return 0;
}

Size of empty class and empty function?

Size of an empty class is non zero(most probably 1), so as to have two objects of the class at different addresses.

http://www2.research.att.com/~bs/bs_faq2.html#sizeof-empty explains it better

 class A{};
void func(){}

std::cout<<sizeof(A)<<std::endl<<sizeof(&func));// prints 1 and 4 on my 32 bit system

How do you determine the size of an object in C++?

To a first order approximation, the size of an object is the sum of the sizes of its constituent data members. You can be sure it will never be smaller than this.

More precisely, the compiler is entitled to insert padding space between data members to ensure that each data member meets the alignment requirements of the platform. Some platforms are very strict about alignment, while others (x86) are more forgiving, but will perform significantly better with proper alignment. So, even the compiler optimization setting can affect the object size.

Inheritance and virtual functions add an additional complication. As others have said, the member functions of your class themselves do not take up "per object" space, but the existence of virtual functions in that class's interface generally implies the existence of a virtual table, essentially a lookup table of function pointers used to dynamically resolve the proper function implementation to call at runtime. The virtual table (vtbl) is accessed generally via a pointer stored in each object.

Derived class objects also include all data members of their base classes.

Finally, access specifiers (public, private, protected) grant the compiler certain leeway with packing of data members.

The short answer is that sizeof(myObj) or sizeof(MyClass) will always tell you the proper size of an object, but its result is not always easy to predict.

Why is the size of an empty class not zero in C#?

  • "0" takes up some space itself to store - if you store it as a 4 byte number it takes up 4 bytes!
  • Of course this information about the class has to take up memory otherwise where would you read it from?

A C# "class" as defined on MSDN

A class is a construct that enables you to create your own custom types by grouping together variables of other types, methods and events. A class is like a blueprint. It defines the data and behavior of a type. If the class is not declared as static, client code can use it by creating objects or instances which are assigned to a variable. The variable remains in memory until all references to it go out of scope. At that time, the CLR marks it as eligible for garbage collection. If the class is declared as static, then only one copy exists in memory and client code can only access it through the class itself, not an instance variable.



Related Topics



Leave a reply



Submit