<< 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{

Questions & Answers

what is phylogeny
Odigie Reply
evolutionary history and relationship of an organism or group of organisms
AI-Robot
ok
Deng
what is biology
Hajah Reply
the study of living organisms and their interactions with one another and their environments
AI-Robot
what is biology
Victoria Reply
HOW CAN MAN ORGAN FUNCTION
Alfred Reply
the diagram of the digestive system
Assiatu Reply
allimentary cannel
Ogenrwot
How does twins formed
William Reply
They formed in two ways first when one sperm and one egg are splited by mitosis or two sperm and two eggs join together
Oluwatobi
what is genetics
Josephine Reply
Genetics is the study of heredity
Misack
how does twins formed?
Misack
What is manual
Hassan Reply
discuss biological phenomenon and provide pieces of evidence to show that it was responsible for the formation of eukaryotic organelles
Joseph Reply
what is biology
Yousuf Reply
the study of living organisms and their interactions with one another and their environment.
Wine
discuss the biological phenomenon and provide pieces of evidence to show that it was responsible for the formation of eukaryotic organelles in an essay form
Joseph Reply
what is the blood cells
Shaker Reply
list any five characteristics of the blood cells
Shaker
lack electricity and its more savely than electronic microscope because its naturally by using of light
Abdullahi Reply
advantage of electronic microscope is easily and clearly while disadvantage is dangerous because its electronic. advantage of light microscope is savely and naturally by sun while disadvantage is not easily,means its not sharp and not clear
Abdullahi
cell theory state that every organisms composed of one or more cell,cell is the basic unit of life
Abdullahi
is like gone fail us
DENG
cells is the basic structure and functions of all living things
Ramadan
What is classification
ISCONT Reply
is organisms that are similar into groups called tara
Yamosa
in what situation (s) would be the use of a scanning electron microscope be ideal and why?
Kenna Reply
A scanning electron microscope (SEM) is ideal for situations requiring high-resolution imaging of surfaces. It is commonly used in materials science, biology, and geology to examine the topography and composition of samples at a nanoscale level. SEM is particularly useful for studying fine details,
Hilary
Got questions? Join the online conversation and get instant answers!
Jobilize.com Reply

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