yesraaj
yesraaj

Reputation: 47910

Is the size of an object needed for creating object on heap?

When compiler need to know the size of a C (class) object: For example, when allocating a C on the stack or as a directly-held member of another type

From C++ Coding Standards: 101 Rules, Guidelines, and Best Practices

Does that mean for a heap allocated object, size is not necessary?

Class C;//just forward declaration
C * objc = new C();

Upvotes: 1

Views: 465

Answers (6)

Michael Burr
Michael Burr

Reputation: 340218

To answer your specific question:

Does that mean for heap allocated object size is not necessary?

Class C;//just forward declaration
C * objc = new C();

C++ will not let you do that.

Even if it could let you perform a 'new' on an incomplete type by magically resolving the size at a later time (I could envision this being technically possible with cooperation from the linker), the attempt will fail at compile time because of at least 2 reasons:

  1. operator new can only be used with complete types. From the C++98 standard 5.3.4 - "[the allocated] type shall be a complete object type, but not an abstract class type or array thereof"

  2. the compiler has no idea what constructors exist (and are accessible), so it would also have to fail for that reason.

Upvotes: 3

The compiler must see the declaration of the class for two reasons: it must know the size it must allocate (as others have already pointed out) but also because the compiler must know how to construct the object: Does it have a default constructor, implicitly defined default constructor, no default constructor? The compiler must know even if the object can be created with a no argument constructor.

Upvotes: 0

anon
anon

Reputation:

As a programmer, you almost never need to know the size of an object in C++. For example:

class A {
    ...  // member data
};

void f() {
    A a;              // allocate on stack
    A * p = new A;    // allocate on heap
}

In neither case is knowledge of the size needed by the programmer - the compiler of course needs to know it.

Note that however you create an object, it's size must be known by the compiler at the point of creation:

class B;     // forward declaration - no size:

void f() {
    B b;              // compilation error
    B * p = new B;    // compilation error
}

Upvotes: 2

1800 INFORMATION
1800 INFORMATION

Reputation: 135295

No. In order to allocate an object on the heap, you must know the size.

Foo *foo=(Foo *)malloc(sizeof(*foo));

Upvotes: 0

Yuval Adam
Yuval Adam

Reputation: 165242

Object size is computed by the new operator:

Object *o = new Object();

You do not need to explicitly tell new the object size, but it does compute it (using sizeof operator) in order to allocate the correct amount of space on the heap.

Upvotes: 2

Iraimbilanja
Iraimbilanja

Reputation:

No, this list is by way of example and not of exclusion. Obviously the object size must be known in heap allocation so the right amount of memory can be allocated.

Upvotes: 5

Related Questions