using the delete operator error


Basically, I don't not how I can delete the new objects I've created in this array. Apparently, the I'm getting is the debug assertion failed
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
#include<iostream>

int main()
{using namespace std;
	int* iSUPERMAN[1];
	int lastnumber = 0;

	//ADD A THOUSAND ELEMENTS
//	iSUPERMAN[lastnumber] = new int (500);
	//declare pointer name
	//declare [array address]
	//declare [new]
	//declare [data type]
	//declare [(initilise)]
int i;
//paassing floats add (f) since decimals are regarded as double
cin >> i;
	for(int lim = 0; lim < i;lim++)
	{lim;
		iSUPERMAN [lastnumber] = new int (500);
		lastnumber++;
	}
	delete []iSUPERMAN;
	
	system("PAUSE");
	return 0;
}


That code isn't going to work for a variety of reasons. Let's say the user enters 5 for the value i. iSUPERMAN is only an array of 1 integer pointer. Therefore the second iteration and beyond will result in undefined behavior. Secondly, since the loop might create multiple integer pointers and iSuperman is an array the delete statement makes no sense. iSUPERMAN itself is an array of integer pointers and should not be deleted at all. It is a stack array. It is the contents of each element that you would want to delete.

1
2
// delete the element but there is only one element in the first place.
delete [] iSUPERMAN[0];


Finally you need to learn how to check for input errors. i is left uninitialized if the cin statement fails.
http://www.parashift.com/c++-faq-lite/input-output.html#faq-15.3
The thing is that new variables at declared at runtime and they need to be deleted
Topic archived. No new replies allowed.