November 1999
Intermediate to advanced
336 pages
6h 29m
English
Allocating and deallocating heap memory on the fly is expensive. From a performance perspective, it is cheaper to work with memory that does not necessitate explicit management. An object defined as a local variable resides on the stack. Stack memory occupied by this object is part of the stack memory set aside for the function in whose scope the object is defined. The alternative to a local object is using new() and delete() to acquire and release heap memory:
void f()
{
X *xPtr = new X;
...
delete xPtr;
}
A better performance choice would be to define a local object of type X:
void f()
{
X x;
...
} // no need to delete x.
In the latter implementation, object x is on the stack. There's no need to allocate it up front, or ...
Read now
Unlock full access