2012-12-16 4 views
0

현재 작업하고있는 프로그램의 목적은 포인터의 동적 배열을 시뮬레이션하여 기본 정수 배열 데이터 형식을 "향상"하는 클래스를 만드는 것입니다. 포인터와 포인터의 배열을 삭제하려고 할 때 "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
int* point = new int[length]; 

길이가 -1부터 시작하므로이 줄의 첫 번째 호출은 new int[0] 일 것입니다. 당신이 length의 의미를 고정하지 주장하면 .. 문제가 될 수 없습니다, 당신은 여기 length+1 원하는

관련없는 점, 당신은 std::vector가 어떻게하는지 살펴 봐야하는 대신 모든 추가 재 할당, 당신은 overallocating 만 재 할당 시도해야한다 공간이 채워지는 경우.

+0

입력 해 주셔서 대단히 감사드립니다. 0에서 기본 길이를 사용하여 다시 작성 했으므로 완벽하게 작동합니다. 나는 아직도 그 이유를 정확히 이해하지 못하지만, 여러분의 의견에 감사 드리며 미래에이 점을 다시 한번 눈여겨 볼 것입니다. 그러나 다시 한번, 귀하의 의견에 대해 대단히 감사드립니다. – rob630

+0

@ rob630 '길이'가 의미하는 것이 혼란 스럽기 때문에 작동하지 않습니다. 배열에 5 개의 원소가 있다면, 길이는 5가되어야하지만, 여러분의 경우에는 4가 될 것입니다. addint 함수에서는 길이가 길이라는 것을 가정하기 때문에 항상 1을 할당합니다. 기본값의 경우 -1, 그렇지 않습니다. –

+0

오 ... 물론입니다.그건 완벽하게 이해가됩니다. 나는 array [5]가 array [x] 인 요소의 관점에서 생각했다. 여기서 x는 0-4의 숫자가 될 것이다. 그것은 어리석은 실수였다. 도와 주셔서 다시 한번 감사 드리며 특히 제 실수를 이해하도록 도와 주셔서 감사합니다. : D – rob630

2

복사 생성자에있는 에 i<length (이하보다 작거나 같음)이 있어야한다고 생각합니다. 그것은 하나의 분명한 문제입니다.

또한 addint() 메서드로 바인딩을 전달합니다. 배열의 마지막 요소는 인덱스 [길이 -1]에 있습니다.

+0

길이 값은 -1로 기본값이 설정되고 "배열"에 숫자가 추가 될 때마다 증가하므로 i <길이로 만들면 배열 크기가 0이면 값이 복사되지 않습니다. <= – rob630

+0

@ user1907309 길이라는 용어를 오용하고 있습니다. 길이는 음수가 될 수 없으며'i

+0

프로젝트에서 길이라고 부르는 것이 요구되었습니다. -1로 시작하고 함수가 호출 될 때마다 실제 값으로 증가합니다. 값이 -1 인 경우 포인터의 값은 null입니다. 아마 여기에 대해 이야기하는 것이 나의 미숙이지만, 이것이 왜이 문제의 원인인지는 잘 모르겠습니다. – rob630

관련 문제