0% found this document useful (0 votes)
37 views13 pages

Dynamic Memory Allocation

Uploaded by

Bhanu Rathi
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
37 views13 pages

Dynamic Memory Allocation

Uploaded by

Bhanu Rathi
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 13

Dynamic Memory Allocation

3/03/11

Dynamic Memory Allocation


In C, functions such as malloc() are used to dynamically allocate memory from the Heap. In C++, this is accomplished using the new and delete operators new is used to allocate memory during execution time returns a pointer to the address where the object is to be stored always returns a pointer to the type that follows the new
2

Easier Dynamic Memory Allocation: new & delete


new corresponds to malloc/calloc in C delete corresponds to free in C The syntax of new has forms: type *pointer = new type; // type is a built-in // or user-defined data type. type *pointer = new type[n]; // type as above The semantics of this syntax is explained next

Operator new Syntax


new DataType new DataType [IntExpression] If memory is available, in an area called the heap (or free store) new allocates the requested object or array, and returns a pointer to (address of ) the memory allocated. Otherwise, program terminates with error message. The dynamically allocated object exists until the delete operator destroys it.

Operator new
2000

char*

ptr;

??? 5000
ptr

ptr = new char; *ptr = B; cout << *ptr;


5000
B

NOTE: Dynamic data has no variable name

Example of new
// Precondition: n is a positive integer // Postcondition: computes and prints out the first n // elements of the Fibonacci sequence void fibonacci(int n){ int *x = new int [n]; // creation of a dynamic array x[0]=1; x[1]=1; for (int i=2;i<n;i++) x[i]=x[i-1]+x[i-2]; cout<<"The Fibonacci sequence of "<<n<<" values are:\n"; for (int i=0;i<n;i++) cout<<"x["<<i<<"]="<<x[i]<<endl; }

Semantics of new
For type *pointer = new type; :
The system allocates dynamically (during execution) a chunk of memory large enough to hold data of the specified type, and returns a pointer pointing to the address of that chunk. This pointer is stored in the user-provided pointervariable pointer.

Semantics of new [n]


For type *pointer = new type[n]; :
The system allocates dynamically an array of n memory chunks, each large enough to hold data of the specified type, and returns a pointer pointing to the address of that chunk. The difference between this type of arrays (called dynamic arrays) and conventional arrays is that the size of the later is constant while the size of the former can vary.

The NULL Pointer


There is a pointer constant called the null pointer denoted by NULL But NULL is not memory address 0. NOTE: It is an error to dereference a pointer whose value is NULL. Such an error may cause your program to crash, or behave erratically. It is the programmer s job to check for this. while (ptr != NULL) { . . . // ok to use *ptr here }

Operator delete Syntax


delete Pointer delete [ ] Pointer The object or array currently pointed to by Pointer is deallocated, and the value of Pointer is undefined. The memory is returned to the free store.

Good idea to set the pointer to the released memory to NULL


Square brackets are used with delete to deallocate a dynamically allocated array.

Operator delete
char* ptr;
2000 5000 ???

ptr = new char; *ptr = B; cout << *ptr; ptr;

ptr

5000
B NOTE: delete deallocates the memory pointed to by ptr

delete

Example
char *ptr ; ptr = new char[ 5 ]; strcpy( ptr, Bye ); ptr[ 0 ] = u; 6000
B y e \0 u // deallocates the array pointed to by ptr // ptr itself is not deallocated // the value of ptr becomes undefined

3000 ptr ??? NULL 6000 ???

delete [] ptr; ptr = NULL;

Syntax and Semantics of delete


The syntax of delete has two forms: delete pointer ; delete [] pointer ; The first releases the memory of the single memory chunk pointed to by pointer The second releases the memory allocated to the array pointed to by pointer.

You might also like