Learning  C++
Home
Tutorials
C++  Programs
Contact  us
Sitemap
Your Ad Here
Your Ad Here
Dynamic  Memory  Allocation


Memory  is  utilized  by  your  program  in  three  different  ways.


1)  All   global   and   static   variables   are   found   in  a  region  known  as  static  memory. These  variables  "live"  throughout  the  running  of  your  program,  and  are  automatically initialized  to  zero.


2)  The  creation  of  automatic  variables  at  function  or  block  scope  occurs  in  an  area known  as  the  stack.  As  each  variable  is  created,  it  is  said  to  be  pushed  onto  the stack,   and   when   it   goes   out  of  scope,  it  gets  popped  off  the  stack.  In  addition, whenever  you  call  upon  a  function,  the  formal  arguments  and  any  auto  variables  that  the function  may  need  are  pushed  onto  the  stack,  and  when  the  function  exits,  these variables  are  popped  from  the  stack.


3)  You  may  allocate  memory  at  execution  time  from  an  area  known  as  the  heap,  or free  memory.  Such  memory  is  always  unnamed,  and  therefore  is  addressed  by  a pointer  that  contains  the  starting  address.  A  data  object  created  here  will  remain  in existence  until  it  is  explicitly  destroyed.  That  is,  the  lifetime  of  an  object  is  directly under our control  and  is  unrelated  to  the  block  structure  of  the  program.



The  C++  keyword  new

The  new  operator  can  be  used  to  allocate  memory  at  execution  time. It  takes  the  following  general  form:



pointer-variable  =  new  data-type ;



Here,  pointer-variable  is  a  pointer  of  type  data-type.  The  new  operator  allocates sufficient  memory  to  hold  a  data  object  of  type  data-type  and  returns  the  address  of   the  object.  



Memory  for  a  single  instance  of  a  primitive  type


int  *ptr1  =  new  int ;

float  *ptr2  =  new  float ;

char  *ptr3  =  new  char ;



Primitive  types  allocated  from  the  heap  via  new  can  be  initialized  with  some  user-specified  value  by  enclosing  the  value  within  parentheses  immediately  after  the  type name.


For  example,

int  *ptr  =  new  int ( 65 ) ;



Memory  for  an  array  of  a  primitive  type

To  get  memory  for  an  array  of  some  primitive  type  using  new,  write  the  keyword  new  followed  by  the  number  of  array  elements  enclosed  within  square  brackets.  


int  *ptr  =  new  int [ 5 ] ;
for ( int  i = 0 ; i < 5 ;  i++ )
cin  >> ptr [ i ] ;



Note  that  it  is  not  possible  to  initialize  the  individual  elements  of  an  array  created  when using  new.  The  best  you  can  do  is  assign  into  them  after  the  creation  has occurred.



The  C++  keyword  delete

When  a  data  object  is  no  longer  needed,  it  is  destroyed  to  release  the  memory space for  reuse.  The  delete  keyword  in  C++  is  used  to  release  the  space  that  was reserved by  new.


The  general  form   of  its  use  is:

delete  pointer-variable ;


The  pointer-variable  is  the  pointer  that  points  to  a  data  object  created  with  new.


For  example,

  delete  ptr ;



How  to  delete  a single  instance  from  the  heap

To  delete  a  single  instance  from  the  heap,  write  the  keyword  delete  followed  by  the name  of  the  pointer  that  points  to  the  heap  space.


For  example,

int  * ptr  =  new  int ;

delete  ptr ;



How  to  delete  an  array  of  instances  from  the  heap

To  delete  an  array  of  instances  from  the  heap,  write  the  keyword  delete  followed  by empty  brackets  and  the  name  of  the  pointer  variable.  


For  example,

int *  ptr  =  new  int [ 10 ] ;

delete  [ ]  ptr ;