Debug assertion failed: _BLOCK_TYPE_IS_VALID(phead->nBlockUse)
I'm new in C++ and I have a problem which I don't seem to be able to solve. The problem seems to be caused by the destructor. When I debug the program I get the message:
Debug Assertion Failed!
File: dbgdel.cpp
line: 52
Expression: _BLOCK_TYPE_IS_VALID(phead->nBlockUse)
I also get an error message in operator= when I use the delete [] temparray; statement.
Debug error!
Heap corruption Detected:
after normal block(#185) at 0x00357440
I hope anybody can help me understand what is going on with my code.
thank you,
CH
// IntArray.h
#ifndef _INTARRAY_H
#define _INTARRAY_H
#include <string>
using namespace std;
class IntArray {
private:
int lowvalue, highvalue, size;
int* temparray;
string st;
public:
IntArray();
IntArray(int n1);
IntArray(int n1, int n2);
IntArray(const IntArray&);
void setName(string s);
int high();
int low();
int arraySize(IntArray ia);
friend ostream& operator<<(ostream& os, const IntArray& ia);
IntArray operator+(IntArray& ia);
IntArray& operator+=(IntArray& ia);
int& operator[](int i);
IntArray& operator=(const IntArray&);
int operator==(IntArray& ia);
int operator!=(IntArray& ia);
~IntArray();
};
#endif
// IntArray.cpp
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
#include <stdlib.h>
#include "intarray.h"
using namespace std;
extern ofstream output;
IntArray::IntArray(){
lowvalue = 0;
highvalue = 9;
temparray = new int[highvalue + 1];
}
IntArray::IntArray(int n1){
lowvalue = 0;
highvalue = n1 - 1;
temparray = new int[n1];
}
IntArray::IntArray(int n1, int n2){
if(n1 <= n2){
lowvalue = n1;
highvalue = n2;
temparray = new int[n2 - n1 + 1];
}
else {//reverses the indexes if lowvalue > highvalue
cout<<"Error: Illegal array bounds"<< endl;
lowvalue = n2;
highvalue = n1;
temparray = new int[n1 - n2 + 1];
}
}
IntArray::IntArray(const IntArray& ia){
lowvalue = ia.lowvalue;
highvalue = ia.highvalue;
temparray = ia.temparray;
}
void IntArray::setName(string s){ st = s;}
int IntArray::high(){ return highvalue;}
int IntArray::low(){ return lowvalue;}
int IntArray::arraySize(IntArray ia){
size = (ia.highvalue - ia.lowvalue) + 1;
return size;
}
ostream& operator<<(ostream& os, const IntArray& ia){
for (int j = ia.lowvalue; j <= ia.highvalue; j++)
os << ia.st << "[" << j << "] = " << ia.temparray[j] << ", ";
return os;
}
IntArray IntArray::operator+(IntArray& ia){
int num = ia.highvalue - ia.lowvalue;
IntArray temp(num + 1);
for(int i = 0; i < num + 1; i++)
temp[i] = temparray[lowvalue++] + ia.temparray[ia.lowvalue++];
return temp;
}
IntArray& IntArray::operator+=(IntArray& ia){
for(int i = lowvalue; i <= highvalue; i++)
temparray[i] += ia.temparray[ia.lowvalue++];
return *this;
}
int& IntArray::operator[](int i){
if ( i > highvalue) {
cout << "Error: subscript out of bounds." << endl;
return temparray[i] = 0;
}
else
return temparray[i];
}
IntArray& IntArray::operator=(const IntArray& ia){
if(highvalue - lowvalue == ia.highvalue - ia.lowvalue) {
if (this != &ia){
//delete [] temparray;//I know I should be using this statement here but I get an error
temparray = new int[highvalue - lowvalue + 1];
int index = ia.lowvalue;
for(int i = lowvalue; i <= highvalue; i++)
temparray[i] = ia.temparray[index++];
}
return *this;
}
else cout << "Error: length mismatch." << endl;
return *this;
}
int IntArray::operator==(IntArray& ia){
if(size == ia.size && temparray == ia.temparray){
return 1;
}
else return 0;
}
int IntArray::operator!=(IntArray& ia){
if(size != ia.size || temparray != ia.temparray){
return 1;
}
else return 0;
}
IntArray::~IntArray(){
delete [] temparray;
}

