0% found this document useful (0 votes)
291 views

Dynamic Memory Allocation

Dynamic memory management in C++ allows allocation of memory during runtime using operators new and delete. The new operator allocates memory in the heap and returns a pointer to that memory. Delete deallocates memory previously allocated by new. Memory can be dynamically allocated for variables, arrays, and class objects. Abstract classes define general concepts that more specific classes can inherit from but cannot be instantiated, containing at least one pure virtual function defined with =0 that derived classes must implement.

Uploaded by

mail.sushilk8403
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
291 views

Dynamic Memory Allocation

Dynamic memory management in C++ allows allocation of memory during runtime using operators new and delete. The new operator allocates memory in the heap and returns a pointer to that memory. Delete deallocates memory previously allocated by new. Memory can be dynamically allocated for variables, arrays, and class objects. Abstract classes define general concepts that more specific classes can inherit from but cannot be instantiated, containing at least one pure virtual function defined with =0 that derived classes must implement.

Uploaded by

mail.sushilk8403
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Dynamic Memory Management

Memory in C++ program is divided into two parts:

 The stack: All variables declared inside the function will take up memory from the stack.
 The heap (free store): This is unused memory of the program and can be used to allocate the
memory dynamically when program runs.

Static memory allocation

Allocation of memory for variables, during compile time itself is known as static memory allocation.
Once the memory is allocated during compile time, the allocated memory cannot be expanded nor be
compressed to accommodate more or less data during program execution.

Dynamic memory allocation

Many times, you are not aware in advance how much memory you will need to store particular
information in a defined variable and the size of required memory can be determined at run time.
Dynamic memory allocation is carried in C++ using two operators “new” and “delete”.

You can allocate memory at run time within the heap for the variable of a given type using “new”
operator which returns the address of the space allocated.

If you are not in need of dynamically allocated memory anymore, you can use “delete” operator,
which de-allocates memory previously allocated by new operator.

Advantages of using “new”

1. It automatically returns the address of correct cast, unlike C’s memory allocation function
“malloc”, “calloc” and “realloc”.
2. It automatically returns the size of the data object for which the memory is allocated, which
requires no “sizeof” operator, making syntax easy to write, read and understand unlike C’s
memory allocation function “malloc”, “calloc” and “realloc”.
3. “new” and “delete” operators cannot be overloaded.

General Syntax for allocating memory dynamically for a single integer variable:

<pointer_variable>=new <data_type>;

Example:

#include<iostream.h>

#include<conio.h>

void main()

{
clrscr();

int *p;

p=new int;

*p=20;

cout<<"the address is="<<p<<endl;

cout<<"the value is ="<<*p<<endl;

getch();

In the above program the statement,

p=new int;

allocates 4 bytes of memory and the starting address of the allocated memory is stored in the pointer
variable “p”.

General Syntax for allocating memory dynamically for array:

<pointer_variable>=new <data_type>[size];

General Syntax for allocating memory dynamically for a class object:

<pointer_variable>=new <class_name>;

Example:

#include<iostream.h>

#include<conio.h>

class A

int a;

public:

A()

cout<<"hello"<<endl;

a=10;

}
void display()

cout<<a;

};

void main()

clrscr();

A *ptr;

ptr=new A;

ptr->display();

getch();

Dynamic Memory Deallocation

The object created by ‘new’ operator has to be deallocated once its purpose it over. This is done by
‘delete’ operator.

General syntax is as follows:

Delete <pointer_variable>

Abstract class
Sometimes implementation of all function cannot be provided in a base class because we don’t know
the implementation. Such a class is called abstract class. For example, let Shape be a base class. We
cannot provide implementation of function draw( ) in Shape, but we know every derived class must
have implementation of draw( ). Similarly an Animal class doesn’t have implementation of move ( )
(assuming that all animals move), but all animals must know how to move. We cannot create objects
of abstract classes.

Abstract classes act as expressions of general concepts from which more specific classes can be
derived. You cannot create an object of an abstract class type; however, you can use pointers and
references to abstract class types.
A class that contains at least one pure virtual function is considered an abstract class. Classes derived
from the abstract class must implement the pure virtual function or they, too, are abstract classes.

A pure virtual function is declared by assigning 0 in declaration. See the following example.
// An abstract class
class Test
{
// Data members of class
public:
// Pure Virtual Function
virtual void show() = 0;

/* Other members */
};

Interface vs abstract class


An interface does not have implementation of any of its methods; it can be considered as a collection
of method declarations. In C++, an interface can be simulated by making all methods as pure virtual.
In Java, there is a separate keyword for interface.

Characteristics of abstract class


1. A class is abstract if it has at least one pure virtual function.
2. We can have pointers and references of abstract class type.
3. If we do not override the pure virtual function in derived class, then derived class also becomes
abstract class.
4. An abstract class can have constructors.

You might also like