Home > Guides > Programming > C/C++

Toggle Open Guide Table of ContentsGuide Contents

Close Table of ContentsGuide Contents

Close Table of Contents

Dynamic Storage Duration

Last updated Jul 2, 2009.

Objects created dynamically during program execution using new-expressions, and destroyed using delete-expressions have dynamic storage duration. Such objects reside on a memory section called free-store memory (also mistakenly called heap memory although the heap is a memory area designated for standard C memory management functions).

The lifetime of an object with dynamic storage duration begins once its initializer has completed. If an object has trivial initialization, its lifetime begins once storage with the proper alignment and size for the object is obtained. The lifetime of an object of type T ends when:

  • If T is a class type with a non-trivial destructor, the destructor call starts.
  • If T is class type with a trivial destructor, once the storage which the object occupies is reused or released.

Allocation and Deallocation Functions

Standard C++ provides the global allocation functions operator new and operator new[], and the global deallocation functions operator delete and operator delete[]. These functions should not be confused with the new operator and the delete operator which you normally use for creating objects dynamically and destroying them:

char *buf=operator new char[MAX];//operator new[]
char * str=new char [MAX]; //new operator

The main difference between the allocation functions and the new operators is that the latter also initialize the allocated object. In other words, a new expression of the form:

string * pstr = new string;

Implicitly invokes the allocation function operator new[sizeof(std::string)], which returns an address of a raw memory buffer that has the proper alignment and size for type std::string. Next, the new operator invokes the constructor of class string, initializing the object *pstr. Similarly, the delete operator call:

delete pstr; //delete operator

First invokes the destructor of std::string and then calls the deallocation function operator delete[] to release the raw memory to the free-store.

Here's a simple way to remember the differences between allocation and deallocation functions on the one hand and the new and delete operators on the other hand. Operator new and operator delete deal with low-level raw memory management. They are the C++ equivalent of C's malloc() and free(), respectively. By contrast, the new operator and delete operator imply object semantics, meaning, initialization after the raw memory has been allocated, and destructor invocation before the raw memory is reclaimed, respectively.

Avoiding Resources Leaks

Objects and variables that are allocated on the free-store persist until they're explicitly released by a subsequent delete operator call. Failing to call delete results in a memory leak in the case of an object with a trivial destructor. Most modern operating systems will automatically reclaim such leaked memory once the program terminates but some embedded operating systems will not. Failing to call delete for an objects with a nontrivial destructor results in undefined behavior because the side-effects of the destructor will never take effect. Remember: releasing the raw storage of an object with dynamic storage duration is only one of a two-phased resource reclaiming procedure. The destructor of an object with dynamic storage duration must also be invoked in order to release additional resources that the object may own. Therefore, failing to call delete or delete[] to destroy a dynamically allocated object or an array of such objects is one of the most common and evasive bugs.

Unlike with objects with static storage duration, the address of an object with dynamic storage duration is determined at runtime. The initial value of the raw memory obtained from an operator new or operator new[] call is indeterminate. Some operating systems initialize the raw memory to binary zeros but it's best to not make any assumptions about it.

Scalar and Array Operators

C++ distinguishes between scalar new and delete, which allocate and deallocate a single object, and new[] and delete[], which allocate and deallocate arrays. For example:

int *p = new int; //scalar new
char *s = new char[1024]; //array new, or new[] for short
Shape *ps=new Triangle;
std::string *pstr=new std::string
delete p; //scalar delete
delete[] s; //array delete, because s was allocated by new[]
delete  ps; //invokes the destructor, then releases raw memory
delete pstr; //ditto

Never confuse the scalar operator with its array version or vice versa; the results in either case are undefined. Here's a simple guideline: if the pointer p was allocated using new[], you must use delete[] p to destroy it. If p was allocated using scalar new, use delete p:

int *p = new int; //scalar new
delete p; //scalar delete
std::string pstr = new string[10]; //new []
delete [] pstr; //delete[]

Related Resources

There are currently no related podcasts. Please check back later.

Emily Nave#TuesdayTrivia: WindowsRT, Tiles and Charms...Oh My! Win "Building Windows 8 Apps with C# and XAML" eBook by Jeremy Likness
By Emily NaveNovember 6, 2012Comments
Software developers know the thing you can count on in your career is change. Microsoft is redefining the way apps are developed -- are you ready to get on board?

Rachel BaylessInformIT's 17 Days of Giveaways
By Rachel BaylessJuly 3, 2012Comments

Get ready for a month full of giveaways. From July 9 through the end of the month, InformIT will be having 17 days of giveaways. Each week has a theme to make sure that there’s something YOU will be excited to win!

John  TraenkenschuhDennis Ritchie--May He Rest in Peace...
By John TraenkenschuhOctober 16, 2011Comments
Time to Remember a True Computing Pioneer...


See More Blogs