Pointer and new Question
What is the difference between:
someClass *ptr = new someClass;
To me the last one looks a lot better, as I have heard the new is a slow command, and that when you use new you also need to have delete. But I am feeling that I am missing something here...
someClass *ptr = &temp;
The first one points to allocated memory, the second one points to an automatic(?).
New dynamically allocates memory that won't be removed until delete is called on the pointer. What happens when temp goes out of scope? What will ptr point to then?
Originally Posted by h3ro
A pointer is just a variable that holds an address. In the first, new allocates memory and returns a pointer to that new memory. The second assigns the address from a stack variable to the pointer.