pointers, new and delete

I'm solving an exercise in which I have to build a stack using a pointer list. I have to define a struct (I named it 'stack') containing an int value and a pointer to next struct element.

Adding a new value to the stack means creating a new stack element, setting it's link to a pointer to the previously uppermost stack element and updating the pointer variable which always points to the uppermost stack element. Taking an element from the stack means returning the value of the uppermost element, deleting it and refreshing the pointer variable which points to the uppermost stack element.

My stack already works fine. The problem is that I don't know how to proper delete stack elements. Does line 86: delete [] tmp; what it should, namely delete the previously uppermost stack element (i.e. freeing the space allocated for it)? How do I properly free the memory allocated in this program?

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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#include <iostream>
#include <string>
#include <sstream>

using namespace std;

// functions
    void push (int element);
    int pop (void);
    int size (void);
    void clear (void);

 // structure definition
    struct stack
    {
        int val;        // value of stack element
        stack * link; // pointer do next lower stack element
    };

// define global stack variables
    stack *TOP = NULL; // pointer to uppermost stack element
    int SIZE = 0; // current number of elements in stack

/////////////////////////////////////////////////////////////////////

int main()
{
    cout << "push(1)" << endl;
        push(1);
    cout << "  push(7)" << endl;
        push(7);
    cout << "    TOP->val " << TOP->val << endl;
    cout << "    push(666)" << endl;
        push(666);
    cout << "      size() " << size() << endl;
    cout << "      TOP->val " << TOP->val << endl;
    cout << "      pop() " << pop() << endl;
    cout << "    size() " << size() << endl;
    cout << "    TOP->val " << TOP->val << endl;
    cout << "    pop() " << pop() << endl;
    cout << "  pop() " << pop() << endl;
    cout << "size() " << size() << endl;

    return 0;
}

/////////////////////////////////////////////////////////////////////

void push (int element)
{
 // check size of STACK, resize it if necessary
    if (0 >= SIZE) // stack empty -> initialize it
    {
        TOP = new stack;
        TOP->val = element;
        TOP->link = NULL;
        SIZE = 1;
    }
 // add 'element' to STACK
    else
    {
        stack *tmp = new stack;
        tmp->link = TOP;
        TOP = tmp;
        TOP->val = element;
        ++SIZE;
    }
}

/////////////////////////////////////////////////////////////////////

int pop (void)
{
 // if stack is empty, return 0 (not the best solution but whatever)
    if (0 >= SIZE)
    {
        SIZE = 0;
        return 0;
    }
 // return uppermost stack element and lower POS by 1
    int element = TOP->val;
    stack *tmp = TOP;
    TOP = tmp->link;
    delete [] tmp;
    --SIZE;
    return element;
}

/////////////////////////////////////////////////////////////////////

int size (void)
{
    return SIZE;
}

/////////////////////////////////////////////////////////////////////

void clear (void)
{
}

///////////////////////////////////////////////////////////////////// 
Last edited on
First of all, your push/pop/size/clear functions should be members of your stack class. Your val and link* items should be a different class ('element' or so). Then, the elements take care of links and values, while the stack class provides the interface (and a pointer to the last element).

On your question: I think most people do it by having each element clean its own memory in the destructor.
Thanks for the suggestions regarding the stack class, but the Beginners Course I'm taking hasn't progressed to custom classes yet (but I'm sure it soon will), so I'll just leave it like I have it for the moment.

What exactly do you mean by "having each element clean it's own memory in the destructor"?
closed account (D80DSL3A)
Line 84 delete [] tmp; is incorrect because only a single stack element is allocated to each stack*, not an array of them. Use delete tmp; instead.
The clear() should iterate through the stack and delete each stack element as it goes, like this:
1
2
3
4
5
6
7
8
9
void clear (void)// this function deletes every element from the stack
{
    while( TOP )
    {
        stack* tmp = TOP;
        TOP = TOP->link;
        delete tmp;
    }
}
Line 84 should do nothing else but delete one single stack element. You confuse it with the clear() function which should delete all stack elements at once.

Line 84 is part of the pop() function which returns the value of the uppermost stack element and then deletes this element, making the previously second-uppermost element the new uppermost element. My question is: Does line 84 really delete the stack element to which tmp is a pointer, or does it just delete the pointer, leaving the stack element itself in place?
Topic archived. No new replies allowed.