When a program is compiled, the size of the data the program will need to handle is
often an unknown factor; in other words there is no way to estimate the memory requirements of the program.

In cases like this you will need to allocate memory dynamically, that is, while the program is running. Dynamically allocated memory can be released to continually optimize memory usage with respect to current requirements.

This in turn provides a high level of flexibility, allowing a programmer to represent dynamic data structures, such as trees and linked lists.

Programs can access a large space of free memory known as the heap. Depending on
the operating system (and how the OS is configured), the heap can also occupy large
amounts of unused space on the hard disk by swapping memory to disk.

C++ uses the new and delete operators to allocate and release memory, and this
means that objects of any type can be created and destroyed. Let’s look at the scenario
for fundamental types first.

simple program of Dynamic Memory Allocation

#include<iostream>
using namespace std;
int main()
{
cout << "nTesting dynamic storage allocation! "
<< endl;

// To allocate storage:

double width = 23.78;
double* ptrWidth = &width;
double* ptrLength = new double(32.54);
double* ptrArea = new double;


// To work with ptrWidth, ptrLength, and ptrArea:


*ptrArea = *ptrWidth * *ptrLength;
delete ptrLength; // Error: The object is still

// in use!
cout << "nWidth : " << *ptrWidth
<< "nLength : " << *ptrLength
<< "nArea : " << *ptrArea << endl;


// To free storage:

delete ptrWidth;             // Error: The object has not
                            // been dynamically reserved

delete ptrLength;        // ok
delete ptrArea;          // ok
delete ptrLength;        // Error: Pointer doesn't
                         // address any object.

ptrLength = new double(19.45);      // ok
                                   // To give a name to a dynamic object:

double& length = *ptrLength;      // Reference
cout << "nNew length : " << length
<< "nCircumference : " << 2 * width * length
<< endl;
return 0; // On terminating the program
}

output in your screen;

A program should make careful use of available memory and always release memory that
is no longer needed.

Failure to do so can impact the performance of your computer system. Memory that is released is available for further calls to new.

Calling new for Fundamental Types

The new operator is an operator that expects the type of object to be created as an argument. In its simplest form, a call to new follows this syntax

ptr = new type;

Where ptr is a pointer to type. The new operator creates an object of the specified
type and returns the address of that object. The address is normally assigned to a pointer
variable. If the pointer belongs to a wrong type, the compiler will issue an error message.

long double *pld = new long double;

This statement allocates memory for a long double type object, that is, sizeof(long double) bytes. The previous call to new does not define an initial value for the new object, however, you can supply a value in parentheses to initialize the object.

pld = new long double(10000.99);

this statement pld points to a memory address containing a long double
type with a value of 10000.99. The statement

cout << *pld << endl;

will output this value

If you like this post, don’t forget to share 🙂

This article is written by our awesome writer
Comments to: What is Dynamic Memory Allocation in cpp

Your email address will not be published. Required fields are marked *

Attach images - Only PNG, JPG, JPEG and GIF are supported.

New Dark Mode Is Here

Sign In to access the new Dark Mode reading option.

Join our Newsletter

Get our monthly recap with the latest news, articles and resources.

By subscribing you agree to our Privacy Policy.

Latest Articles

Explore Tutorials By Categories

About

Codeverb is simply an all in one interactive learning portal, we regularly add new topics and keep improving the existing ones, if you have any suggestions, questions, bugs issue or any other queries you can simply reach us via the contact page

Login

Welcome to Codeverb

Ready to learn something new?
Join Codeverb!

Read Smart, Save Time
  •  
    Strength indicator
  •  
  •  
    Log In | Lost Password