Wednesday 12 August 2015

Dynamic Memory and "new" and "delete" Operators

As you may remember from your lesson on arrays, we initially declared the size of the array in our code. However, what if, at run time, the user only wanted to enter five elements in the ten element array we declared? That would be a waste of space. Conversely, if our array is smaller than what the user wants, that would pose another problem. To overcome this, we use the concept of dynamic memory, where the array size is entered by the user during the running of the application.

Dynamic Memory

          Until now, we have only had as much memory as we have requested in declarations of variables, arrays etc
          The size of all of them was fixed BEFORE the execution of the program.
          What if we need a variable amount of memory
          That can only be determined DURING the program execution (runtime).
          For example, in case that we need user input to determine the necessary amount of space?

The answer is dynamic memory
We can accomplish this using the operators new and delete

Operators "new" and "new[]"

          new: to request dynamic memory
          new is followed by a data type
          and optionally the number of elements required within brackets [].
          Returns a pointer to the beginning of the new block of assigned memory.
                pointer = new type // to assign memory to contain one single element of type
               or
               pointer = new type [elements]  // to assign a block (an array) of elements of type.

int * b;
b = new int[5];
          The operating system will assign space for 5 elements of type int
          And will return a pointer to its beginning
          That has been assigned to b.
          Therefore, now, b points to a valid block of memory with space for 5 int elements. 


Why use pointers as arrays?

•the size of an array must be a constant value
•Which limits its size to what we decide at the moment of designing the program before its execution
•Whereas the dynamic memory allocation allows assigning memory during the execution of the program
•using any variable, constant or combination of both

Dynamic Memory Implementation


•The dynamic memory is generally managed by the operating system
•Shared between several applications
•So there is a possibility that the memory exhausts.
•If so, the operating system cannot assign the memory that we request with the operator new
•A null pointer will be returned.
•For that reason it is recommended to always check to see if the returned pointer is null after a call to new

int * b;
   b = new int [5];
   if (b == NULL) {
       // error assigning memory. Take
     //measures.
   }; 

Operators "delete"

          Once the dynamic memory is no longer needed it should be freed
          So that it becomes available for future requests of dynamic memory.
          The operator delete exists for this purpose
                delete pointer;
                or
               delete [] pointer;

          The first expression should be used to delete memory allocated for a single element
          The second one for memory allocated for multiple elements (arrays).
          In most compilers both operators are equivalent

"new" and "delete"

Take a look at an example code to see the implementation of the operators:





About the Author

Unknown

Editorial Team

Post a Comment

 
Codexify © 2015 - Designed by Templateism.com