-2

I have written this small program:

#include <iostream>

using namespace std;

class a{
};

int main ()
{
a *obj=new a();
cout<<sizeof(obj)<<endl;
cout<<sizeof(*obj)<<endl;
delete obj;
}

Below is the output that i have got:

> ./a.out
4
1
>

I can understand that its using 4 bytes for storing the address of the object. But what i dont understand is that 1 byte. since its an empty class i have got a doubt of what is the purpose of that 1 byte(size of the object).

My second question is will the default constructor be called? if yes what would it basically do?

Vijay
  • 65,327
  • 90
  • 227
  • 319
  • Learn from here: [**What is the size of an object of an empty class?**](http://stackoverflow.com/questions/621616/c-what-is-the-size-of-an-object-of-an-empty-class) – Grijesh Chauhan Feb 25 '13 at 12:46
  • This also has second question which does not have an answer yet in the given so called duplicate link. – Vijay Feb 25 '13 at 12:51
  • @sarthi Ok please consider my comments my help And This link may asnwer second part [**What does a compiler add to an empty class declaration?**](http://stackoverflow.com/questions/2659895/what-does-a-compiler-add-to-an-empty-class-declaration) and [**Are empty constructors always called in C++?**](http://stackoverflow.com/questions/5097545/are-empty-constructors-always-called-in-c) – Grijesh Chauhan Feb 25 '13 at 12:56
  • @Sarthi AND I am not down voter but I voted up. – Grijesh Chauhan Feb 25 '13 at 12:57

4 Answers4

1

The byte is there because the size of an object can't be 0 in C++. It's a dummy byte. Think about an array of a - if the size of a was 0, all objects would be located at the same address.

Theoretically, an empty default constructor is called. In practice, it's optimized out because it has no observable behavior.

Luchian Grigore
  • 253,575
  • 64
  • 457
  • 625
0

First sizeof prints out size of a pointer 32-bit in 32-bit systems. Second sizeof prints out size of a content which is aligned to 1 byte because different objects must create in different addresses.

However you can have a zero size class even with non-virtual methods:

class A
{
    A()
    {
       cout << "Hi" << endl;
    }
    char unused[];
    // Note: Putting at least a virtual method increases the size to 4 bytes.
};

and

cout << sizeof(A) << endl; // Prints out 0

More, this code shows all A objects are located in same memory address:

A a1;
A a2;

cout << &a1 << endl;
cout << &a2 << endl;

Output:

0x22ff00

0x22ff00

Community
  • 1
  • 1
masoud
  • 55,379
  • 16
  • 141
  • 208
0

Each object must have a distinct address, so an object in C++ has a minimum size of 1.

The default constructor does nothing.

Klas Lindbäck
  • 33,105
  • 5
  • 57
  • 82
0

what is the purpose of that 1 byte(size of the object)

In C++, all distinct objects must have a distinct addresses. In order to achieve this, all objects are at least as large as the smallest addressable unit - that is, one byte.

My second question is will the default constructor be called?

Technically, yes. If you don't declare your own default constructor, then one will be implicitly generated if its needed. The implicit constructor will default-construct all members and base sub-objects. However...

if yes what would it basically do?

Since the class has no members or sub-objects that need initialising, the implicit constructor is trivial: it does nothing.

Mike Seymour
  • 249,747
  • 28
  • 448
  • 644
  • Any reason for the downvote? I'm fairly sure my answer is correct, although perhaps it glosses over a few marginally relevant details such as empty base classes. – Mike Seymour Feb 25 '13 at 14:48