One more thing: even though they are usually used together, pointers and new/delete operators are not tied together.
So, take for example the following code (don't take it as an good example, as it contains bad code, only take it as "it can be done").
void aFunction(){ new int; //we allocate an integer from the heap int* pOne = new int; int* pTwo = pOne; //pTwo actually point at the same place in memory as pOne *pOne = 1; //and, because of that, *pTwo is 1 as well for(int i=0;i<10;++i) { if(i%2==0) *pOne++ else (*pTwo)++; } // now, you'd think that because, at the beginning of the for loop, the pOne and pTwo pointed at the same place in heap //and the loop runned for 10 times, both pOne and pTwo would still point to the same place in memory,and the value of both *pOne and *pTwo is 11 //but this in not it. *pTwo is 6 and *pOne is... we don't know delete pOne; //this is bad, really bad delete pTwo; //this releases the heap allocated by the line int* pOne = new int; delete new int; //an pathetic attempt to release the heap memory allocated by the first line of this function (new int;)}
Now, what happened is as follows:
The line (*pTwo)++ does what we're expecting of it and that is increases by one the value stored on the heap that the pTwo pointer points at. But *pOne++, due to the order the operators are evaluated, first increases the value of pOne (the value of the pointer itself) by evaluating pOne++ and then, using the new value of pOne, returns whatever value is stored on the heap in the location that the pOne pointer now points, by evaluating the * operator. That is why, after the for loop, *pTwo still points to the variable we allocated on the heap and that has the value increased 5 times (so now it's 6) and pOne points to somewhere else in the memory.
Also, please note that, no matter how many pointers you have pointing at the same place in memory, you only need to release the memory once and take care not to use the pointers with that value anymore, as this will still access the old location of the heap which may now be used by the computer for something else.
One more thing: the first line of the function
new int;
does what it is supposed to do: allocated the memory to hold an integer value from the heap and returns the location of that variable to the program. However, since you are not storing it in any pointer, the value of the location (the memory address) is lost, so you cannot release the memory allocated. It will remain there alocated forever (untill your program ends). It is called a memory leak;
the last line of the function, no matter how weird, is valid
delete new int;
what it does is first allocate a new int from the heap, return the address of that variable to the expresiion, which the delete operator then uses to release that memory that we just allocated. It's valid, though silly. (if instead of int we had a class in there, both the constructor and the destructor of that class would have runned in that line).