<< Chapter < Page Chapter >> Page >

Example 1:

int *pPointer;

pPointer = new int;

Example 2:

delete pPointer;

Example 3: A 10-element integer array can be created and assigned to arrayPtr as follows:

int *arrayPtr = new int[10];

This array is deleted with the statement

delete [] arrayPtr;

Stack versus heap

A stack is a region of memory where applications can store data such as local variables, function calls, and parameter information.

The programmers have no control over the stack. C++ automatically handles placing and removing data to and from stack.

The heap or free store, is an area of memory that is available to application for storing data whose existence and size are not known until run-time.

Notice that when we use new operator, we can allocate a piece of memory on the heap and when we use delete operator, we can deallocate (free) a piece of memory on the heap. In other words, we can manage the memory allocation on the heap explicitly through new and delete operators.

The syntax for using the new operator is

pointer = new data_type;

For example, to declare an int pointer iPointer that points to a heap variable, you use the following statements:

int* iPointer;

iPointer = new int;

The syntax for using the delete operator is

delete pointer_name;

For example, to delete the heap memory pointed to by the iPointer pointer, you use the statement delele iPointer;.

Deleting the contents of an array stored on the heap also requires a slightly different syntax. You must append two brackets to the delete keyword using the syntax delete[] array_name; .

Notice that the delete operator does not delete the pointer itself. Rather, it deletes the contents of the heap memory address pointed to by a pointer variable. You can reuse the pointer itself after calling the delete operator. The pointer still exists and points to the same heap memory address that it did before calling the delete operator.

Example

#include<iostream.h>

int main( )

{

double* pPrimeInterest = new double;

*pPrimeInterest = 0.065;

cout<<“The value of pPrimeInterest is: “

<<*pPrimeInterest<<endl;

cout<<“The memory address of pPimeInterest is:”

<<&pPrimeInterest<<endl;

delete pPrimeInterest;

*pPimeInterest = 0.070;

cout<<“The value of pPrimeInterest is: “

<<*pPrimeInterest<<endl;

cout<<“The memory address of pPrimeInterest is: “

<<&pPrimeInterest<<endl;

return 0;

}

The output of the above program:

The value of pPrimeInterest is: 0.065

The memory address of pPrimeInterest is: 0x0066FD74

The value of pPrimeInterest is: 0.070

The memory address of pPrimeInterest is: 0x0066FD74.

Note: The above program declares the pPrimeInterest pointer on the heap and assigns to it a value of 0.065. Then the delete operator deletes the heap address that stores the value of 0.065. Finally, a new value is added to the heap address. You can see that after the delete statement executes, the pPimeInterest pointer still point to the same memory address.

Example

In the following program, we can create some objects of the class Stocks on the stack or on the heap and then manipulate them.

#include<iostream.h>

class Stocks{

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now




Source:  OpenStax, Programming fundamentals in c++. OpenStax CNX. Jul 29, 2009 Download for free at http://cnx.org/content/col10788/1.1
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Programming fundamentals in c++' conversation and receive update notifications?

Ask