Wednesday, September 24, 2014

Difference between an array and dymanic memory pointer in C++

It is known to all that the least thing any programmer would like to deal with is dealing with Pointers. May Java be blessed !
Suppose I have two declarations:

1)  int array[10] //an array of 10 integer elements

2) int  *pArray ;
    pArray = new int [10];

The major difference is that for the normal array (1) , the size of the array is constant and has to be decided before the execution. However the dynamic memory allocation (2) give you more flexibity by allowing you to assign memory during the execution of the program (runtime) using any variable or constant value as its size.

 How to access elements from our declaration:
  array[2] // accessing the third element
  pArray[2] or  *(pArray+2)  // accessing the third element ....

No comments:

Post a Comment