Using the delete operator

My main function asks the user to enter a number then it calls this function using that number.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
int fact(const int x)
{
	double * table = new double[x];
	table[0] = 1;
	cout << "\n0! = 1\n";
	for (int i = 1; i <= x; i++)
	{

		table[i] = i * table[i-1]; 
		cout << i << "! = " << table[i] << "\n";
	}
	cout << "\n";
	delete [] table;
	return 0;
}


I ran the program without line 13 a few times and everything worked great. But then I remembered that when you use new you should match it with delete. Once I added line 13 I get an error: Debug Error! HEAP CORRUPTION DETECTED!

If anyone knows what the problem is I would really appreciate it.
It's most probably the way you allocate and delete table. Some of the information in this article may help you: http://cplusplus.com/forum/articles/17108/

Debug Error! HEAP CORRUPTION DETECTED!

Someone has a melodramatic debugger...
You loop past the end of the array. The index can only go as high as x - 1, yet your for loop condition allows i == x and then references table[i].
Thank you for your help.
Topic archived. No new replies allowed.