Can you declare a pointer on the heap?

jkeys picture jkeys · Aug 13, 2009 · Viewed 8.6k times · Source

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?

Answer

AraK picture AraK · Aug 13, 2009
int** ppint = new int*;
*ppint = new int;

delete *ppint;
delete ppint;