delete error

I tried to delete the dynamic pointer age. The first one semed to delete something else. What am I doing wrong?

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
#include <iostream>

using namespace std;

int main()
{
    int* age;
    int i, n;
    cin >> i;
    age = new int[i];
    for(n = 0; n<i; n++)
    {
        cout << "Enter age: ";
        cin >> age[n];
    }
    cout << endl;
        int x = 0;
    for(n=0; n<i; n++)
    {
        cout << "age " << ++x << ": " << age[n] << endl;
    }
    for(n=0; n<i; n++)
    {
        delete age[n];
        cout << "age " << age[n] << ": deleted" << endl;
    }
    return 0;
}



Edit:

Is this the right way to delete dynamic pointers? If so, how the <bleep> does delete [] age; work? I read a bunch on dynamic pointers and new and delete. I can't visualize/understand/see how that piece of code works.

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
#include <iostream>

using namespace std;

int main()
{
    int* age;
    int i, n;
    cin >> i;
    age = new int[i];
    for(n = 0; n<i; n++)
    {
        cout << "Enter age: ";
        cin >> age[n];
    }
    cout << endl << endl;
        int x = 0;
    for(n=0; n<i; n++)
    {
        cout << "age " << ++x << ": " << age[n] << endl;
    }
    for(n=0; n<i; n++)
    {
        cout << "deleteing age : " << (n + 1) << endl;
        delete [] age;
    }
    /*FOR TEST*/
    ////////////
    x = 0;
    for(n=0; n<i; n++)
    {
        cout << "age " << ++x << ": " << age[n] << endl;
    }
    ////////////
    return 0;
}


4
Enter age: 10
Enter age: 20
Enter age: 30
Enter age: 40


age 1: 10
age 2: 20
age 3: 30
age 4: 40
deleteing age : 1
deleteing age : 2
deleteing age : 3
deleteing age : 4
age 1: 4063632
age 2: 4063632
age 3: 30
age 4: 40

Process returned 0 (0x0)   execution time : 12.891 s
Press any key to continue.
int main()
{
int* age;
int i, n;
cin >> i;
age = new int[i];
for(n = 0; n<i; n++)
{
cout << "Enter age: ";
cin >> age[n];
}
cout << endl;
int x = 0;
for(n=0; n<i; n++)
{
cout << "age " << ++x << ": " << age[n] << endl;
}

delete []age;




return 0;
}

dude delete is only for objects created at runtime ("new" operator is used) and yes they also need to be under pointers.
firix's example has it right.

You should have exactly 1 delete[] for every 1 new[]. If you only new[] once (to allocate space for age), then you only delete[] once (with delete[] age;).
Two problems:

1. Consider your last for loop in the first code sample you posted:

1
2
3
4
5
6
    for(n=0; n<i; n++)
    {
        delete age[n];
        cout << "age " << age[n] << ": deleted" << endl;
    }
    return 0;

You are attempting to delete the contents of your array. This causes problems because the contents of your array are not pointers.

2. In the same block of code, you attempt to access the contents of you array after having made an attempt to delete them.

The solution is to use something similar to this:

1
2
3
4
5
6
    for(n=0; n<i; n++)
    {
        cout << "age " << age[n] << endl;
    }
	delete [i]age;
	cout << "array deleted" << endl;

delete [i]age;

This line doesn't really make any sense (it should be delete[] age; and firix and I mentioned).

+1 to the rest of what you said, though.
Thanks Disch; I’ll keep that in mind.
to understand the delete operator
A good example of

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
int main()
{
	char **pr;
	int k;

	cout << "How many will enter the name:";
	cin >> k;

	pr = new char*[k];

	char name[20];


	for (int i = 0; i < k; i++) {
		cin >> name;
		pr[i] = new char[strlen(name) +1];
		strcpy(pr[i], name);

	}

	cout << "\n********************************************************\n";
	cout << "\nyou entered the names\n";


	for (int i = 0; i < k; i++)
		cout << pr[i] << " ";
	cout << endl;


	
	for (int i = 0; i < k; i++)
		delete []pr[i];
	delete []pr;

	return 0;
}
Thanks for your help on this, everyone. :o)
It does not matter....
Topic archived. No new replies allowed.