## Creating Objects on the Stack and Heap

To explain how objects are created on the stack and on the heap we will use the `Quad` class. You can find the definition in `\$ROOTSYS/tutorials/``quadp/``Quad.h` and `Quad.cxx`. The `Quad` class has four methods. The constructor and destructor, `Evaluate` that evaluates `ax**2 + bx +c`, and `Solve` which solves the quadratic equation `ax**2 + bx +c = 0`.

`Quad.h` :

```class Quad {
public:
Quad(Float_t a, Float_t b, Float_t c);
Float_t Evaluate(Float_t x) const;
void Solve() const;
private:
Float_t fA;
Float_t fB;
Float_t fC;
};
```

`Quad.cxx`:

```#include <iostream.h>
#include <math.h>

fA = a;
fB = b;
fC = c;
}
Cout <<"deleting object with coeffts: "<< fA << "," << fB << "," << fC << endl;
}
return fA*x*x + fB*x + fC;
}
Float_t temp = fB*fB - 4.*fA*fC;
if ( temp > 0. ) {
temp = sqrt( temp );
cout << "There are two roots: " << ( -fB - temp ) / (2.*fA)
<< " and " << ( -fB + temp ) / (2.*fA) << endl;
} else {
if ( temp == 0. ) {
cout << "There are two equal roots: " << -fB / (2.*fA) << endl;
} else {
cout << "There are no roots" << endl;
}
}
}
```

Let us first look how we create an object. When we create an object by:

```root[] `Quad my_object(1.,2.,-3.);`
```

We are creating an object on the stack. A FORTRAN programmer may be familiar with the idea; it is not unlike a local variable in a function or subroutine. Although there are still a few old timers who do not know it, FORTRAN is under no obligation to save local variables once the function or subroutine returns unless the SAVE statement is used. If not then it is likely that FORTRAN will place them on the stack and they will "pop off" when the RETURN statement is reached. To give an object more permanence it has to be placed on the heap.

```root[]` .L Quad.cxx`
root[] `Quad *my_objptr = new Quad(1.,2.,-3.);`
```

The second line declares a pointer to `Quad` called `my_objptr`. From the syntax point of view, this is just like all the other declarations we have seen so far, i.e. this is a stack variable. The value of the pointer is set equal to

```new Quad(1.,2.,-3.)`;`
```

`new`, despite its looks, is an operator and creates an object or variable of the type that comes next, `Quad` in this case, on the heap. Just as with stack objects it has to be initialized by calling its constructor. The syntax requires that the argument list follow the type. This one statement has brought two items into existence, one on the heap and one on the stack. The heap object will live until the delete operator is applied to it.

There is no FORTRAN parallel to a heap object; variables either come or go as control passes in and out of a function or subroutine, or, like a COMMON block variables, live for the lifetime of the program. However, most people in HEP who use FORTRAN will have experience of a memory manager and the act of creating a bank is a good equivalent of a heap object. For those who know systems like ZEBRA, it will come as a relief to learn that objects do not move, C++ does not garbage collect, so there is never a danger that a pointer to an object becomes invalid for that reason. However, having created an object, it is the user's responsibility to ensure that it is deleted when no longer needed, or to pass that responsibility onto to some other object. Failing to do that will result in a memory leak, one of the most common and most hard-to-find C++ bugs.

To send a message to an object via a pointer to it, you need to use the "-`>`" operator e.g.:

```root[] `my_objptr->Solve();`
```

Although we chose to call our pointer `my_objptr`, to emphasize that it is a pointer, heap objects are so common in an object-oriented program that pointer names rarely reflect the fact - you have to be careful that you know if you are dealing with an object or its pointer! Fortunately, the compiler won't tolerate an attempt to do something like:

```root[]` my_objptr.Solve();`
```

Although this is a permitted by the CINT shortcuts, it is one that you are strongly advised not to follow! As we have seen, heap objects have to be accessed via pointers, whereas stack objects can be accessed directly. They can also be accessed via pointers:

```root[]` Quad stack_quad(1.,2.,-3.);`
root[]` Quad *stack_ptr = &stack_quad;`
root[]` stack_ptr->Solve();`
```

Here we have a `Quad` pointer that has been initialized with the address of a stack object. Be very careful if you take the address of stack objects. As we shall see soon, they are deleted automatically, which could leave you with an illegal pointer. Using it will corrupt and may well crash the program!

It is time to look at the destruction of objects. A destructor is a special C++ function that releases resources for (or destroy) an object of a class. It is opposite of a constructor that create the object of a class when is called. The compiler will provide a destructor that does nothing if none is provided. We will add one to our Quad class so that we can see when it is called. The class names the destructor but with a prefix ~ which is the C++ one's complement i.e. bit wise complement, and hence has destruction overtones! We declare it in the .h file and define it in the `.cxx` file. It does not do much except print out that it has been called (still a useful debug technique despite today's powerful debuggers!).

Now run root, load the Quad class and create a heap object:

```root[] `.L Quad.cxx`
root[] `Quad *my_objptr = new Quad(1.,2.,-3.);`
```

To delete the object:

```root[] `delete my_objptr;`
root[] `my_objptr = 0;`
```

You should see the print out from its destructor. Setting the pointer to zero afterwards is not strictly necessary (and CINT does it automatically), but the object is no more accessible, and any attempt to use the pointer again will, as has already been stated, cause grief. So much for heap objects, but how are stack objects deleted? In C++, a stack object is deleted as soon as control leaves the innermost compound statement that encloses it. Therefore, it is singularly futile to do something like:

```root[] `{  Quad my_object(1.,2.,-3.); }`
```

CINT does not follow this rule; if you type in the above line, you will not see the destructor message. As explained in the Script lesson, you can load in compound statements, which would be a bit pointless if everything disappeared as soon as it was loaded! Instead, to reset the stack you have to type:

```root[] `gROOT->Reset();`
```

This sends the Reset message via the global pointer to the ROOT object, which, amongst its many roles, acts as a resource manager. Start ROOT again and type in the following:

```root[] `.L Quad.cxx`
root[] `Quad my_object(1.,2.,-3.);`
root[] `Quad *my_objptr = new Quad(4.,5.,-6.);`
root[] `gROOT->Reset();`
```

You will see that this deletes the first object but not the second. We have also painted ourselves into a corner, as `my_objptr` was also on the stack. This command will fail.

```root[] `my_objptr->Solve();`
```

CINT no longer knows what `my_objptr` is. This is a great example of a memory leak; the heap object exists but we have lost our way to access it. In general, this is not a problem. If any object will outlive the compound statement in which it was created then a more permanent pointer will point to it, which frequently is part of another heap object. See Resetting the Interpreter Environment in the chapter “CINT the C++ Interpreter”.