delete[] gives error

the delete[] statment gives error

Error : HEAP CORRUPTION DETECTED: after normal block(#176) at 0x0057B3B0
CRT detect that the application wrote to memory after end of heap buffer


my code :

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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
# include<iostream>
# include<math.h>
# include<Windows.h>

using namespace std;

void main()
{
	HANDLE hConsole;
	hConsole = GetStdHandle (STD_OUTPUT_HANDLE);

	SetConsoleTextAttribute
		(hConsole, FOREGROUND_BLUE | FOREGROUND_INTENSITY);

	double *coff=NULL,*power=NULL;
	int nofd=0,degree=0;
	double fofx=0,x=0;
	char ct;
	int nod=0;

	do{

		std::cout<<"\nEnter the degree of equation : ";
		cin>>degree;

		coff= new (nothrow) double[degree] ;
		power=new (nothrow) double[degree];
		//For memory exception
		if(coff == NULL)
		{
			std::cout<<"\nInsufficent Memory \nExit_Faliure!!!\n\n";
			exit(1);
		}

		for(int i=degree ; i>=0 ; i--)
		{
			*(power+i)=i;
			//cout<<"\na["<<i<<"] = "<<*(power+i);
		}


		//input loop
		for(int i=degree ; i>= 0 ; i--)
		{
			std::cout<<"\nEnter co-efficent of x^"<<i<<" : ";
			cin>>*(coff+i);
		}

		//showing the equation
		std::cout<<"\n\nEquation : ";
		for(int i=degree ; i>=0 ; i--)
		{
			SetConsoleTextAttribute
				(hConsole, FOREGROUND_GREEN | FOREGROUND_INTENSITY);

			if(nod > degree)
			{
				cout<<" 0";
			}
			else if( *(coff+i) == 0)
			{
				continue;
			}
			else if(i==0)
			{
				std::cout<<*(coff+i);
				std::cout<<" ";
			}
			else if(*(coff+i) == 1)
			{
				std::cout<<"x^"<<*(power+i);
				std::cout<<" + ";			
			}
			else if(i == 1)
			{
				std::cout<<*(coff+i)<<"x ";
				std::cout<<" + ";
			}
			else
			{
				std::cout<<*(coff+i)<<"x^"<<*(power+i);
				std::cout<<" + ";
			}
			nod++;
		}

		//derivatives
		do
		{
			SetConsoleTextAttribute
				(hConsole, FOREGROUND_RED | FOREGROUND_INTENSITY);

			std::cout<<"\n\nEnter the number of derivatives you want to take(less than or equal to "<<degree<<") : ";
			cin>>nofd;

		}while(nofd>degree+1);

			
			int o=1;//to put the dash sign to show derivative

			for(int i=1 ; i<= nofd ; i++)//starting of full for loop
			{
				SetConsoleTextAttribute
					(hConsole, FOREGROUND_RED | FOREGROUND_INTENSITY);
				std::cout<<"\n\nGive the value of x to put in derviative of equation : ";
				cin>>x;
				SetConsoleTextAttribute
					(hConsole, FOREGROUND_GREEN | FOREGROUND_INTENSITY);
				std::cout<<"\n\nf";

				for(int k=0 ; k < o ; k++)
				{
					std::cout<<"\'";
				}

				std::cout<<"(x) : ";
				nod=0;
				for(int j=degree ; j>=0 ; j--)//main working loop to find f'(x)
				{
					SetConsoleTextAttribute
						(hConsole, FOREGROUND_GREEN | FOREGROUND_INTENSITY);
					if(nod > degree)
					{
						cout<<" 0";
					}
					else if( *(coff+j) == 0)//if co-efficent is zero do not display anything
					{
						continue;
					}
					else if(*(power +j) <= 0 )//if powerer =0 do not display anything
					{
						cout<<" ";
						continue;
					}
					else if(j == 0)//display only the co-efficent because the x^0=1
					{
						std::cout<<*(coff+j);
						std::cout<<" ";
					}
					else if(*(power+j) == 1)
					{
						cout<<" + ";
						std::cout<<(*(coff+j) * (*(power+j)));
						fofx+=(*(coff+j) *= (*(power+j))) * (pow(x,*(power + j)-1));
					}
					else 
					{
						if(j== degree)
						{
							cout<<"";
						}
						else
						{
							cout<<" + ";
						}
						std::cout<<(*(coff+j) * (*(power+j)))<<"x^"<<(*(power+j)-1);
						fofx+=(*(coff+j) *= (*(power+j))) * (pow(x,*(power + j)-1));
					}

					*(power+j) -=1;

				}//end of printing of f'(X) loop.main(working loop to find f'(x))
				SetConsoleTextAttribute
					(hConsole, FOREGROUND_BLUE | FOREGROUND_INTENSITY);
				std::cout<<"\n(dy/dx)^"<<i<<" = "<<fofx;
				o++;
				fofx=0;
				nod++;

			}//ending of the full for loop

		cout<<"\n\n";
		std::cout<<"\nTo clear screen and find derivative again press \"f\"";
		std::cout<<"\nTo find derivative again press \"a\"";

		cin >>ct;

		if(ct == 'a' || ct == 'A')
		{
			system("CLS");		
			
		}
		else if(ct == 'f' || ct == 'F')
		{
			cout<<endl;
			for(int p=0 ; p<80 ; p++)
				cout<<"_";
			cout<<endl;
			for(int p=0 ; p<80 ; p++)
				cout<<"*";
			cout<<endl;
			for(int p=0 ; p<80 ; p++)
				cout<<"_";
			ct='a';
		}
	}while(ct == 'A' || ct == 'a');

	delete[] power;
	delete[] coff;
}
closed account (o1vk4iN6)
Says exactly what it means. You allocated memory somewhere and you wrote past the bounds of memory you allocated it with. You are modifying memory you don't actually own hence the corruption.
Last edited on
Valid indices for an array are 0 to size-1 where size is the number of elements in the array.

*(power+i) is the same as power[i]...

When i is the number of elements in the array, the above results in an out-of-bounds array access. You do this several times in your code.
Topic archived. No new replies allowed.