views:

268

answers:

6

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();
+5  A: 

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.

+2  A: 

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.

Yuval A
I believe that the question is a little more precise in the sense that it seems to ask whether a forward declaration for the type suffices to create an object in the heap. sizeof() is calculated by the compiler through knowledge of the type and thus the size, you do not need to know it, but the compiler does.
David Rodríguez - dribeas
+2  A: 

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
}
anon
Implicitly, the size is known in both cases. Just not necessarily by you - but the compiler will give an error if it doesn't know enough to work it out
1800 INFORMATION
A: 

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

Foo *foo=(Foo *)malloc(sizeof(*foo));
1800 INFORMATION
The question is tagged C++, not C.
anon
This is valid in C++ - for example we see in the source for VS C++ runtime the new operator calls through to malloc for the memory allocation so the size is known implicitly at the time of allocation
1800 INFORMATION
Even here "you" as a programmer do not know the size. It is a compiler who knows it (you ask it and pass the answer to malloc)
Suma
Foo *foo=(Foo *)malloc(sizeof(Foo));Also, just because one compiler simply wraps malloc, doesn't mean another does. Also, if Foo has a constructor you just created a Foo without running it.
Dolphin
Usually I assume that if the compiler knows something on my behalf, this is essentially the same as if I had known it, even if I didn't actually print out the value of that thing to the screen. For example we might have any number of local variables for the calculation of a temporary value - you don't have to look at the value in a debugger to say that you "know" that value, it is probably enough to just reference that variable in your code.
1800 INFORMATION
Besides my point was that it was valid C++, I don't really want to get into some kind of Socratic discussion about the meaning of the word "know". Given that it is valid C++, the compiler must needs know the size of the object whether or not you are allocating the object on the heap using new or malloc or any other kind of object. I am using malloc here as an example, and to show explicitly that the size is required - the "new" version does not show that
1800 INFORMATION
A: 

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.

David Rodríguez - dribeas
+3  A: 

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.

Michael Burr