It means a program can obtain required
memory at runtime. Run time means when
the program starts executing not compile
In C++ for dynamic memory allocation we
have two uninary operators.
It is used to allocate the memory space when
It is also used to create objects of all types,
including a class name.
You can allocate memory to a pointer only at
runtime. Syntax is:
◦ Pointer-variable = new data-type
the data type may be a valid C++ data type
Pointer-variable is a pointer of type data-type
The type of allocating memory must be same to
the receiving pointer. it means, you can allocate
only integer type memory to an integer type
Int *p = new int; Int *p = new char;
To assign value to a pointer we have to pass a
value using “( )” .
Syntax will be:
◦ *p =100;
int *p =new int;
*p =200 int *p =new int(200);
When an object, is not in use we must free the
memory captured by that object. So to free the
captured memory is done by “delete” operator.
The syntax is:
◦ Delete pointer-variable.
Int *p = new int;
We can use the keyword const for pointers
before the type, after the type, or in both
places. For example, all the following
declarations are legal:
◦ const int * p;
◦ int * const q;
◦ const int * const r;
Each of the above having different meaning
and different tasks.
*p is a pointer to a constant integer. The
value that is pointed to can‟t be changed.
We can say the int pointed to a constant.
„q‟ is a constant pointer to an integer. The
integer can be changed, but „q‟ can‟t point to
„q‟ is constant, it can‟t point to anything else.
„r‟ is a constant pointer to a constant integer.
The value that is pointed to can‟t be changed,
and „r‟ can‟t be changed to point to anything