writing over memory after end of heap

hey guys, i wrote ths program. it is compiling and working but after it gives me the results i want it gives me an error which says the application wrote to memory after end of heap. i know what it means but i dont know how to solve this . can you help? thank you

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
 #include <iostream>
using namespace std;
int 
main (int argc, char ** argv)
{
	int n=0;
	int i=0;
	int j=0;
	int count=0;
	int m=0;
	int * a = new int [n];
	cout<<"please enter the number of entries you want in your sequence"<<endl;
	cin>>n;
	cout<<"please enter the sequence of numbers you want"<<endl;
	while (i<n){
		cin>>m;
		a[i]=m;
		i++;
	}

	while (j < (n-2))
	{
		if ( a[j]==0 &&  a[j+1]==0 && a[j+2]==1 && j<(n-2) && (j+1)<(n-1) && (j+2)<n)
		{
			cout<<"this sequence contains a 001"<<endl;
			count++;
		}
		
		j++;
	}

	cout<<"the number of times that the sequence 001 was found is"<<endl;
	cout<<count;
	delete a;
	* a= NULL;
	return 0;
}
This statment is invalid * a= NULL; because you deleted the integer memory space that a points to. You wan't this a= NULL;

You also wan't this statment below line 13 int * a = new int [n]; or n will always equal 0;

a is a pointer to an array so you need to use this to delete a delete [] a;
Last edited on
thank you :D
Topic archived. No new replies allowed.