28.12.2014 Views

Dynamic Memory Allocation Suppose we want to write a test ...

Dynamic Memory Allocation Suppose we want to write a test ...

Dynamic Memory Allocation Suppose we want to write a test ...

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

<strong>Dynamic</strong> <strong>Memory</strong> <strong>Allocation</strong><br />

<strong>Suppose</strong> <strong>we</strong> <strong>want</strong> <strong>to</strong> <strong>write</strong> a <strong>test</strong> averaging program that will<br />

average any number of <strong>test</strong>s. How do <strong>we</strong> s<strong>to</strong>re the individual <strong>test</strong>s<br />

in memory if <strong>we</strong> do not know the number of variables <strong>to</strong> define.<br />

To solve this, <strong>we</strong> need <strong>to</strong> allow program <strong>to</strong> create its own variables<br />

“on the fly”. This is called dynamic memory allocation and is only<br />

possible thought the use of pointers.<br />

To dynamically allocate memory means that a program, while<br />

running, asks the computer <strong>to</strong> set aside a chunk of unused memory<br />

large enough <strong>to</strong> hold a variable of a specific data type.<br />

Let’s say that a program requires the creation of an integer<br />

variable. It will make a request <strong>to</strong> the computer. When the<br />

computer fills this request, it finds and sets aside a chunk of<br />

unused memory large enough <strong>to</strong> hold an integer, it then gives the<br />

program the starting address of the chunk of memory. The program<br />

can only access the newly allocated memory through its address,<br />

so a pointer is required.<br />

The way a C++ program requests dynamically allocated memory is<br />

through the new opera<strong>to</strong>r.<br />

Assume a program has a pointer <strong>to</strong> an int defined as:<br />

int *iptr;<br />

Here how memory is dynamically allocated<br />

iptr= new int;<br />

This statement is requesting that the computer allocates enough<br />

memory for a new int variable. The operand of the new opera<strong>to</strong>r<br />

is the data type of the variable being created.<br />

Once the statement executes, iptr will contain the address of the<br />

newly allocated memory.


A value may be s<strong>to</strong>red in this new variable by dereferencing the<br />

pointer:<br />

*iptr= 25.<br />

Any other operation may be performed on the new variable by<br />

simply using the dereference opera<strong>to</strong>r.<br />

cout > *iptr;<br />

<strong>to</strong>tal= +*iptr;<br />

A more practical use of the new opera<strong>to</strong>r is <strong>to</strong> dynamically create<br />

an array<br />

aptr= new int[100];<br />

Once the array is created, <strong>we</strong> can use the subscript notation <strong>to</strong><br />

access it.<br />

Using the new opera<strong>to</strong>r <strong>to</strong> dynamically allocate a structure:<br />

aptr= new int[100];<br />

for (int count =0; count


In order <strong>to</strong> dynamically allocate memory <strong>to</strong> a structure variable, <strong>we</strong><br />

use the opera<strong>to</strong>r new.<br />

Example:<br />

Circle *circle //pointer <strong>to</strong> a Circle structure<br />

circle = new Circle //dynamically allocate a struct.<br />

delete Circle circle;

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!