tags:

views:

149

answers:

4

I'm interested in the nuts and boltw of C++ and I wondered what actually changes when an object is instantiated. I'm particularly interested if the functions are then added to memory, if they are there from runtime or if they are never stored in memory at all.

If anyone could direct me to a good site on some of the core bolts of C and C++, I'd love that too.

Thanks, Jo

+1  A: 

The functions are all stored in the executable itself, so they're loaded from the get-go.

Any memory needed by that object for member variables is allocated.

If you're wondering how the function knows which object it's being called on, there's basically an invisible "this" pointer that's passed as the first parameter to the function.

David
Even if the object has no members whatsoever, it is still allocated some memory so as to ensure that all objects have unique memory addresses.
Skizz
+6  A: 

Not sure of a good web site, but Inside The C++ Object Model is a pretty good book.

At least in the usual case, the member functions exist completely independent of any instance of the class. Instead, an instance of the class is a struct containing the (non-static) data members of the object. If the class has at least on virtual function, the object will also contain a pointer to a vtable, which is basically an array of pointers to functions.

When a member function is called, the address of that object is passed as a hidden parameter to the function (many compilers reserve a register just for it) and in the function it's referred to as this.

Jerry Coffin
I haven't read it, but +1 for the TOC alone.
uncle brad
A: 

When an object is instantiated;

  1. Memory is allocated
  2. The constructor is executed
  3. The vtable is "set up"

Number 3 is the reason why constructors cannot call virtual functions in derived classes (when the constructor is executed the vtable is not yet "set up").

You can get the same effect using a letter/envelope idiom. See Advanced C++ Programming Styles and Idioms for this and more.

uncle brad
vtables, while not guaranteed to actually exist by the standard, tend to be initialized as part of the constructor framework. vtables tend to be initialized from the least derived to most derived (in the same order in which constructors are initialized).
Nathan Ernst
@Nathan - Thanks, I've been programming on Windows too long. Can you tell me of a compiler/platform that does not use vtables?
uncle brad
+1  A: 

A common case is:

  1. Memory is allocated by calling operator new. This function will most probably be already in memory, it's needed a lot.
  2. The constructor of the class is called. This code could already be in memory. If not, the call to this function page-faults. The OS notes, and loads the appropriate page from your executable into RAM. It then tells the OS to retry. ( 2A. The ctor arranges for virtual functions to be callable - often by writing a vtable pointer )
  3. Chances are that the page with the constructor also contains the other members of your class. Those can then be called too. But if they are on another page, calling them may cause another page fault and another load. If your compiler put a vtable on a different page, the use of that vtable may also cause a page fault.

The advantage of such a load-on-demand mechanism is that an OS can avoid loading the code for class CPrinter if the user never intends to print his document.

MSalters