2013-10-22 5 views
1

delete[] *iopszString;에 오류가있는 이유를 이해할 수 없으므로 해결할 수 있습니까? 나는 마지막으로 [] 모든 작품을 삭제 생략하면 1 3 aaa마지막 삭제시 오류가 발생했습니다

하지만 교환 포인터하기 위해 내가 이전 점을 삭제할 필요가 있기 때문에 이해가되지 않습니다 :

입력을 시도해보십시오. The code

// Notepad.cpp 

#include <iostream> 
#include <fstream> 
#include <string> 

using namespace std; 

// Method definition 
void addText(char** iopszString); 

void main() 
{ 

    // Const definition 
    int const ADD = 1; 
    int const UPDATE = 2; 
    int const DELETE = 3; 
    int const SAVE = 4; 
    int const EXIT = 5; 

    // Variable definition 
    int nUserCode; 

    // Code section 

    // Gets the user code 
    cout << "Enter the code: " << endl; 
    cin >> nUserCode; 

    // + "\0" so 1 minimum!!! 
    char* iopszString = new char[1]; 
    iopszString = ""; 

    // Runs until the exit code 
    while (nUserCode != EXIT) 
    { 
     // Checks the exit code 
     switch (nUserCode) 
     { 
      case ADD: 
      { 
       addText(&iopszString); 
       cout << iopszString << endl; 
       break; 
      } 
      case UPDATE: 
      { 

       break; 
      } 
      case DELETE: 
      { 

       break; 
      } 
      case SAVE: 
      { 

       break; 
      } 
      default: 
      { 
       cout << "Wrong code, try again" << endl; 

       break; 
      } 
     } 

     // Gets the user code 
     cout << "Enter the code: " << endl; 
     cin >> nUserCode; 
    } 

    // Delete the string cuz heap 
    delete[] iopszString; 
} 

void addText(char** iopszString) 
{ 
    // Variables definition 
    int nAddLength; 

    // Code section 

    // Gets the new length 
    cout << "Enter the length of the added string: " << endl; 
    cin >> nAddLength; 

    // Always remember - the length you want+1!! 
    char* szNewString = new char[nAddLength+1]; 

    // Gets the new string 
    cout << "Enter the new string which you want to add: " << endl; 
    cin >> szNewString; 

    // Creating a new string (result) 
    char* szResult = new char[nAddLength+1+strlen(*iopszString)]; 

    // Copies the old string to the new 
    strcpy(szResult, *iopszString); 
    strcat(szResult, szNewString); 

    // Deletes the new string cuz we already copied 
    delete[] szNewString; 

    // Exchange pointers 
    //strcpy(*iopszString, szResult); <--- never 

    // The problem! 
    delete[] *iopszString; 

    // Exchange pointer 
    *iopszString = szResult; 
} 
+0

무엇 오류 메시지입니다. 이자형? – bstamour

+2

'std :: string'을 사용하고'void main'을 사용하는 것을 강력히 권장합니다. – chris

답변

6

버그는이 두 줄에 있습니다

char* iopszString = new char[1]; 
iopszString = ""; 

당신은 new 새로운 메모리를 할당하고 있고 포인터 iopszString에 위치를 저장합니다. 그런 다음 문자열 리터럴 ""의 위치를 ​​해당 포인터에 할당하므로 포인터 자체의 값이 변경됩니다.으로 할당하지 않은 메모리 위치와 소유하지 않은 다른 위치를 가리 킵니다. 따라서 할당 한 메모리의 포인터 (메모리 누수)가 손실되고 ""의 위치에 대한 포인터에서 delete[]을 호출하면 과 함께 할당 할 수 없으므로 아무 것도 해제 할 수 없기 때문에 충돌이 발생합니다.

당신은 아마 쓸 의미 :. 단지 당신이 '\0'에 할당하기 때문에 유효한, 빈, 제로로 끝나는 문자열로 바뀝니다 첫 char의 값을 설정합니다

char* iopszString = new char[1]; 
iopszString[0] = '\0'; 

+0

문제가 해결되었습니다. 감사합니다! 하지만 설명을 간단하게 해주시겠습니까? –

+0

@desmondmiles 설명을 개선하려고 노력했지만 도움이되기를 바랍니다. –

+0

와우 정말 이해했습니다! 감사 :-) –

관련 문제