Tuesday, July 6, 2010

C++ constructor (ctor) and destructor (dtor) review

It’s been a long time I haven’t touched C++. I only realized this when I found that I couldn’t differentiate between
Stuff *s = new Stuff;
Stuff s;
If you use new, some memory from the heap will be allocated for s. And the ctor of Stuff is called.

If you had too much C#, you may think that "Stuff s" does almost nothing other than declaring something. But in C++, "Stuff s" actually call the ctor of Stuff. And the memory required by s is taken from the stack. And the ctor of Stuff is called.

When do you release the memory then? If you use new, you have to delete s somewhere in the program. If you don’t use new, the dtor of Stuff will be called before the current scope exits. The call to the dtor is actually generated by the compiler.

Compile the following to see clearly what happens when you are not using new:
class Stuff {
    Stuff() {
      printf("\nctor called.");
    ~Stuff() {
      printf("\ndtor called.");

void test() {
  printf("\nInside test(). Before Stuff s.");
  Stuff s;
  printf("\nInside test(). After Stuff s.");

void main() {
  printf("\nInside main(). Before calling test().");
  printf("\nInside main(). After calling test().");

  printf("\n\nPress Enter to exit.");

Did you notice that dtor is called even though you did not do anything? The following disassembled output shows that the compiler generated the call to dtor.

No comments:

Post a Comment