Click here to Skip to main content
15,039,037 members
Please Sign up or sign in to vote.
2.50/5 (2 votes)
See more:
#include <<iostream>>

using namespace std;


int main()
{

int **p;
p=new int[10];

delete[] p;
return 0;
}


Is that right way to allocate memory dynamically using double pointer.

Please explain.
Posted
Comments
[no name] 10-Mar-14 2:46am
   
To allocate an array of ints you do not need pointer to pointer. Is there a reason you are using "double pointer"?.

1 solution

No. You don't need a "pointer-to-a-pointer-to-an-integer", because you are only allocating an array of integers: a "pointer-to-an-integer" - and most modern compilers will give you an error when you try, because you cannot convert an "int*" to an "int**".

If you want to use the code as it is, then remove one '*' from the declaration of "p".
Otherwise, you have to be moire sophisticated in your coding:

C++
int main()
    {
    int **p, i;
    p=new int*[10];
    for(i = 0; i < 10; i++)
        {
        p[i] = new int[5];
        }

    ...

    for(i = 0; i < 10; i++)
        {
        delete[] p[i];
        }
    delete[] p;
    return 0;
    }
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900