Dynamic Memory Allocation
Dynamic Memory Allocation
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.
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.
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.
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;
getch();
p=new int;
allocates 4 bytes of memory and the starting address of the allocated memory is stored in the pointer
variable “p”.
<pointer_variable>=new <data_type>[size];
<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();
The object created by ‘new’ operator has to be deallocated once its purpose it over. This is done by
‘delete’ operator.
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 */
};