tags:

views:

502

answers:

4
 class B
{
   public:
     int a;
     void fn();
}

If I create an object of B, using

B* pb = new B;

Where is the memory of fn() locate?

Is there a pointer in object that pointing at the memory loaction of fn()?

If yes, why sizeof(B) returns the value as if there is no pointer in object at all?

+2  A: 

There will only be a pointer stored for a virtual function (in the vtable), not for non-virtual functions.

Shane MacLaughlin
+12  A: 

Where is the memory of fn() locate?

Since it's a normal function, somewhere in the code section of your program. This location is the same for all instances of the class. In fact, it has got nothing to do with the instantiation of B via pb.

Is there a pointer in object that pointing at the memory loaction of fn()?

No. For a normal member function this isn't required since the address is known at compile time (or, at the latest, at link time); it therefore doesn't have to be stored separately at runtime.

For virtual functions, the situation is different. Virtual function pointers are stored in an array (called “virtual function-pointer table” or “vtable” for short). Each class has one such vtable and each instance to a class stores a pointer to that vtable. This is necessary because if a pointer/reference of type Base points to a sub-class Derived, the compiler has no way of knowing which function to call; rather, the correct function is calculated at runtime by looking it up in the associated vtable. The vtable pointer is also evident in the sizeof the object.

Konrad Rudolph
If fn() were virtual, there would be a section of memory somewhere (called a vtable) that would include a pointer to B::fn(). There would be only one for the class, not one for each class object, and so would not show up in sizeof(B).
David Thornley
David: well. There's only one vtable per class, true. However, each instance holds a pointer to that vtable and the pointer size *does* indeed show up in `sizeof(B)`. I'll clarify this.
Konrad Rudolph
+1  A: 

1) B* pb = new B will allocate the memory on the heap. That means, among other things, that you will need to clean it up yourself via delete operator. Alternatively, you can place the pointer inside the smart pointer (shared_ptr, auto_ptr. scope_ptr) and let it do the clean up.

2) Because pointer is type, pointing to an object or null, has a defined size, usually equal to int.

3) fn() is NOT a virtual function, hence no memory is allocated in the object to vtable.

+4  A: 

This:

class B
{
   public:
     int a;
     void fn();
};

Is for all practical purposes equivelant to the C code:

struct B
{
   int a;
};

void fn(B* bInstance);

Except in the C++ version bInstance is replaced with the this pointer. Both function's memory exists on the stack. So converting to the struct equivelant, what do you think the sizeof(B) would be?

Doug T.
Make the function argument `const` to be more precise – and perhaps put it in an own namespace. That might distract from you point, though. ;-)
Konrad Rudolph