2014-07-22 9 views
0

생성자에서 메모리를 할당 한 다음 소멸자에서 []을 (를) 삭제하지만, 나는 _Block_Type_Is_Valid (pHead->nBlockUse)" Error이됩니다. 나는 세 가지의 규칙은을 찾아왔다. 내 프로그램에서 나는 단지 하나의 인스턴스를 가지고 있고 Copy Constructor 나 Copy Assignment Operator도 사용하지 않기 때문에 명시 적으로 선언 된 소멸자가 필요하다고 생각했다. 생성자에서 char 배열의 값을 복사하고 무엇이 잘못 되었는 지 알 수 없습니다. 다음은 프로그램 코드의 간단한 목록입니다. 헤더 :삭제자 내에서 []을 (를) 삭제하여 생성자 내에 할당하십시오.

class CTest 
{ 
private: 
    char *TestTable; 
    int TestTableLength; 
    std::chrono::steady_clock::time_point StartPoint; 
    std::chrono::steady_clock::time_point EndPoint; 
    std::chrono::steady_clock::time_point CheckPoint; 
    std::chrono::system_clock::duration d; 
public: 
    CTest(char *SignTable); 
    ~CTest(); 
    void NewCombination(); 
    bool AskToPlay(); 
    bool AskForNewCombination(); 
    void Play(); 
}; 

출처 :

CTest::CTest(char *SignTable) 
{ 
    int SignTableLength = 0; 
    for (int i = 0; *(SignTable + i) != '\0'; i++) 
    { 
     SignTableLength++; 
    } 
    TestTableLength = SignTableLength ; 
    TestTable = new char[TestTableLength]; 
    for (int i = 0; *(SignTable + i) != '\0'; i++) 
    { 
     *(TestTable + i) = *(SignTable + i); 
    } 
} 


CTest::~CTest() 
{ 
    delete[] TestTable; 
} 

void CTest::NewCombination() 
{ 
    int tmpInt; 
    char tmpChar; 
    if (TestTable != NULL) 
    { 
     for (int i = 0; i < TestTableLength - 1; i++) 
     { 
      tmpInt = rand() % (TestTableLength - i); 
      tmpChar = TestTable[TestTableLength - 1 - i]; 
      TestTable[TestTableLength - 1 - i] = TestTable[tmpInt]; 
      TestTable[tmpInt] = tmpChar; 
     } 
    } 
} 

bool CTest::AskToPlay() 
{ 
    char play; 
    std::cout << "Do you want to play again ?" << std::endl << "If yes press 'y' else press something else" << std::endl; 
    std::cin >> play; 
    if (play == 'y') return true; 
    else return false; 
}; 

bool CTest::AskForNewCombination() 
{ 
    char newcom; 
    std::cout << "Do you want me to create new combination?" << std::endl << "If yes press 'y' else press something else" << std::endl; 
    std::cin >> newcom; 
    if (newcom == 'y') return true; 
    else return false; 
}; 

void CTest::Play() 
{ 
    StartPoint = std::chrono::steady_clock::now(); 
    CheckPoint = std::chrono::steady_clock::now(); 
    std::cout << "3\t"; 
    d = CheckPoint - StartPoint; 
    while (std::chrono::duration_cast<std::chrono::milliseconds>(d).count() < 1000) 
    { 
     CheckPoint = std::chrono::steady_clock::now(); 
     d = CheckPoint - StartPoint; 
    } 
    std::cout << "2\t"; 
    while (std::chrono::duration_cast<std::chrono::milliseconds>(d).count() < 2000) 
    { 
     CheckPoint = std::chrono::steady_clock::now(); 
     d = CheckPoint - StartPoint; 
    } 
    std::cout << "1" << std::endl; 
    while (std::chrono::duration_cast<std::chrono::milliseconds>(d).count() < 3000) 
    { 
     CheckPoint = std::chrono::steady_clock::now(); 
     d = CheckPoint - StartPoint; 
    } 
    std::cout << "START!" << std::endl; 
    StartPoint = std::chrono::steady_clock::now(); 
    for (int i = 0; i < (TestTableLength) ; i++) 
    { 
     std::cout << *(TestTable + i) << " "; 
    } 
}; 

홈페이지 :

char Signs[] = { '1', '2', '3', '4', '5', '6', 'q', 'w', 'e', 'r', 'f', 'g' }; 
CTest Test(Signs); 

int _tmain(int argc, _TCHAR* argv[]) 
{ 
    srand(time(NULL)); 
    while (Test.AskToPlay()) 
    { 
     if (Test.AskForNewCombination()) Test.NewCombination(); 
     Test.Play(); 
    } 

    Test.~CTest(); 
    return 0; 
} 
+2

'std :: string'을 사용하십시오. – chris

+7

아마도 복사 생성자를 인식하지 않고 사용하고 있습니다. 이 클래스를 사용하여 오류를 일으키는 예제를 제공해야합니다. 객체가 복사 불가능한 경우 객체에 코드를 구현해야합니다. [다음과 같은 메커니즘이 있습니다] (http://stackoverflow.com/questions/2173746/how-do-i-make-this-c-object-non-copyable). –

+2

이것은 문제가 아니지만 'SignTable [i]'대신'* (SignTable + i)'를 사용하는 이유가 있습니까? – wolfPack88

답변

2

당신은 main의 끝에서 명시 적으로 소멸자를 호출하고 있습니다. 완료되지 않아야하며 프로그램 종료시 소멸자가 자동으로 호출됩니다 (즉, Test 오브젝트가 범위를 벗어날 때). 명시 적으로 소멸자라고하는 것은 할당 된 메모리를 해제하고 프로그램이 종료되면 소멸자가 다시 호출되어 메모리가 다시 해제되어 관찰 된 오류가 발생합니다.

+0

고마워, 나는 방금 수업을 쳐다보고 있는데, 나는 파괴자를 호출하고 프로그램이 끝나는 것이 프로그래머의 의무라고 잘못 생각했다. –

+0

@ user3023499 여기에 해당하지 않는 ** 동적 ** 사용자 개체 할당 (운영자 삭제 호출)은 프로그래머의 의무입니다. ** 자동 ** 객체가 있습니다. 이는 범위를 벗어날 때 소멸된다는 것을 의미합니다 (소멸자 호출 포함). – SomeWittyUsername