현재 작업하고있는 프로그램의 목적은 포인터의 동적 배열을 시뮬레이션하여 기본 정수 배열 데이터 형식을 "향상"하는 클래스를 만드는 것입니다. 포인터와 포인터의 배열을 삭제하려고 할 때 "Windows에서 project4.exe의 중단 점을 트리거했습니다."포인터를 삭제하려고하면 중단 점이 발생합니다.
이는 project4의 버그를 나타내는 힙 손상으로 인한 것일 수 있습니다. EXE하거나로드 DLL 중 어느.
이것은 인해 project4.exe 포커스가있을 F12 가압 사용자 일 수있다. 출력 창보다 진단 정보를 가질 수있다
. "
class Array
{
private:
int length;
int* data;
public:
Array();
Array(const Array &cpy);
~Array();
bool addint(int toadd);
bool deletelast();
int getlength();
friend ostream& operator<<(ostream &out, const Array &n);
};
ostream& operator<<(ostream &out, const Array &n);
Array::Array()
{
length = -1;
data = NULL;
}
Array::Array(const Array &cpy)
{
length = cpy.length; //value of length is copied
if (length < 0)
data = NULL;
else
{
data = new int [length];
for (int i=0; i<=length; i++)
data[i] = cpy.data[i];
}
}
Array::~Array()
{
if (length != 0)
delete [] data;
else
delete data;
data = NULL;
}
bool Array::addint(int toadd)
{
length ++;
int* point = new int[length];
for (int i=0; i < length; i++)
point[i] = data[i];
point[length] = toadd;
if (length != 0)
delete [] data;
data = point;
point = NULL;
return true;
}
bool Array::deletelast()
{
int* temppoint;
if (length > 0)
temppoint = new int [length-1];
else
temppoint = new int[0];
for (int i=0; i<length; i++)
temppoint[i] = data[i];
if (length == 0)
temppoint[0] = 0;
length --;
delete [] data;
data = temppoint;
temppoint = NULL;
return true;
}
void menu(Array var)
{
int selection=0,
input;
bool success;
Array* arrcpy;
while (selection != 3)
{
if (var.getlength() == -1)
{
cout << "What would you like to demonstrate?" << endl << "1) Add an integer " << endl
<< "2) Exit" << endl << "Enter your selection: ";
cin >> selection;
if (selection == 2)
selection = 4;
}
else
{
cout << endl << "Now what would you like to demonstrate?" << endl << "1) Add an integer " << endl
<< "2) Delete the last entered integer" << endl << "3) Copy constructor" << endl << "4) Exit" << endl << "Enter your selection: ";
cin >> selection;
}
if (selection==1)
{
cout << endl << "The length of the array before adding a new value is: " << var.getlength() + 1 << endl;
cout << "Please enter the integer that you wish to add: ";
cin >> input;
success = var.addint(input);
if (success)
cout << endl << "The data input was a success!" << endl << "The length of the array is now: "
<< var.getlength() + 1 << endl << "The new value of the array is: " << var << endl;
else
cout << endl << "The input failed" << endl;
}
if (selection == 2)
{
cout << endl << "The lenght of the array before the deletion is: " << var.getlength() + 1 << endl
<< "and the value held in the array is: " << var << endl;
success = var.deletelast();
if (success)
cout << endl << "The data deletion was a success!" << endl << "The length of the array is now: "
<< var.getlength() + 1 << endl << "The new value of the array is: " << var << endl;
else
cout << endl << "The deletion failed" << endl;
}
if (selection == 3)
{
cout << endl << "The lenght of the array being copied is: " << var.getlength() + 1 << endl
<< "and the value held in the array is: " << var << endl;
arrcpy=new Array(var);
cout << endl << "The length of the copied array is: " << arrcpy->getlength() +1 << endl
<< "and the value contained in the array is: " << *arrcpy;
delete arrcpy;
}
}
}
이것은 내가 갖고있는 문제와 관련된 모든 관련 소스 코드입니다. 이 경우에는 delete
연산자와 delete []
연산자의 모든 인스턴스에서이 중단 점 오류가 발생하고 내가 잘못하고 있는지 확실하지 않습니다.
편집 : 길이 값을 -1 대신 0으로 기본 설정하고 모든 것이 작동합니다!
입력 해 주셔서 대단히 감사드립니다. 0에서 기본 길이를 사용하여 다시 작성 했으므로 완벽하게 작동합니다. 나는 아직도 그 이유를 정확히 이해하지 못하지만, 여러분의 의견에 감사 드리며 미래에이 점을 다시 한번 눈여겨 볼 것입니다. 그러나 다시 한번, 귀하의 의견에 대해 대단히 감사드립니다. – rob630
@ rob630 '길이'가 의미하는 것이 혼란 스럽기 때문에 작동하지 않습니다. 배열에 5 개의 원소가 있다면, 길이는 5가되어야하지만, 여러분의 경우에는 4가 될 것입니다. addint 함수에서는 길이가 길이라는 것을 가정하기 때문에 항상 1을 할당합니다. 기본값의 경우 -1, 그렇지 않습니다. –
오 ... 물론입니다.그건 완벽하게 이해가됩니다. 나는 array [5]가 array [x] 인 요소의 관점에서 생각했다. 여기서 x는 0-4의 숫자가 될 것이다. 그것은 어리석은 실수였다. 도와 주셔서 다시 한번 감사 드리며 특히 제 실수를 이해하도록 도와 주셔서 감사합니다. : D – rob630