I'm not really understanding why delete needs to be used since new is used in the function, it should delete anyway after the function is finished right?
Isn't ps just a pointer to another pointer? Is * pstr and ps the same thing? I thought they were not because * pstr is in the function. Sorry if I seem lost - i'm really having a problem understanding pointers. Any help would be appreciated. If the questions don't make any sense I really apologize.
Okay, that helps, and the tutorial is very good - I wish there were more like this since the style is vary easy to understand, but I'm still not 100% clear on the relationship between ps and *pstr
It's my understanding that when you create a dynamic array you would use this form:
type_name * pointer_name = new type_name [num_elements]
then you will delete it with:
delete  pointer_name
The code in the example above looks like this to me:
char * pstr = newchar[n + 1];
delete  ps;
I can see that *pstr is in a function and the value is assigned to ps, but I'm unclear on how new and delete work in this case. Doesn't delete clear the memory specifically assigned to ps? Even though they have the same value they are not using the same block of memory, right? Or are they?
Don't get mixed up by the names. pstr is the name of the pointer in your function. Your function is returning the address of the pointer to the calling function. This return value is assigned to the variable named ps in main(). This variable ps now points to the memory you allocated inside your function. So to delete this memory you need to use the variable that points to the memory block you want to delete, in this case the variable is named ps. The variable named pstr no longer exists, it was local to the function, the memory is now accessed by the variable named ps.