Search code examples
c++dynamic-memory-allocationheap-memory

Can you declare a pointer on the heap?


This is the method for creating a variable on the heap in C++:

T *ptr = new T;

ptr refers to a pointer to the new T, obviously. My question is, can you do this:

T *ptr = new T*;

That seems like it could lead to some very, very dangerous code. Does anyone know if this is possible/how to use it properly?


Solution

  • int** ppint = new int*;
    *ppint = new int;
    
    delete *ppint;
    delete ppint;