May 2012
Intermediate to advanced
679 pages
16h 56m
English
The C++ has added (over C) two keywords “new” and “delete”. They are operators as per syntax rules. They are useful in memory allocation and de-allocation.
The task of function malloc() is now handed over to operator new. The syntax of operator new is as follows:
<pointer_to_name> = new <name> [ <name_initializer> ];
The “new” operator tries to create an object <name> by allocating sizeof(<name>) bytes in the heap. Consider a declaration:
float *ft1;
Now we can allocate memory to store a float variable as
ft1 = new float;
Later on we may assign value 22.5 to it as
*ft1=22.5 ;
Alternately, we can combine these two actions in one as shown below.
ft1 = new float(22.5) ;
Well allocating memory for ...
Read now
Unlock full access