new / delete

I have a couple questions about the following code from the C++ Primer book I'm working through related to the new and delete functions. Here is the code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <iostream>

using namespace std;

char * buildstr(char c, int n);

int main()
{
    int times;
    char ch;

    cout << "Enter a character: ";
    cin >> ch;
    cout << "Enter an interger: ";
    cin >> times;

    char *ps = buildstr(ch, times);
    cout << ps << endl;
    delete [] ps;
    ps = buildstr('+', 20);
    cout << ps << "-DONE-" << ps << endl;
    delete[] ps;

    return 0;
}

char * buildstr(char c, int n)
{
    char * pstr = new char[n + 1];
    pstr[n] = '\0';
    while (n-- > 0 )
        pstr[n] = c;
    return pstr;
}


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.
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?

No, only local statically allocated variables are "deleted" when the function ends. The memory allocated with new doesn't get deleted until you call delete.

Isn't ps just a pointer to another pointer?

No, ps is being assigned the value returned by the function.

Is * pstr and ps the same thing?

No.

You may want to try studying this tutorial about pointers. http://augustcouncil.com/~tgibson/tutorial/ptr.html
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:

1
2
char * pstr = new char[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.
Thanks for taking your time to help! I was getting mixed up by the names, but your explanation helps to clear this up.
Topic archived. No new replies allowed.