2016-03-06 82 views
-2

不尋找在這一個答案,但一些方向將不勝感激。我所看到的所有地方以及我嘗試過的所有答案都不能解決問題。C++動態內存分配造成損壞的堆

我有指令聲明「動態分配變量到用戶輸入的內容,不能使用placeHolder變量(userInput [256}])來捕獲輸入。

我原來的代碼是:

int main(){ 

    char cont = 'y'; 
    char *userInput = nullptr; 

    while (cont == 'y' || cont == 'Y') 
    { 

     int ptrLength = 0; 
     userInput = new char[ptrLength]; 

     cout << "Please enter a word or phrase: ";//2. Asks the user to enter any string (any sequence of characters) 
     while (cin.peek() != '\n'){ 
      cin >> userInput[ptrLength]; 
      ptrLength++; 
     } 
     //1. You must use a pointer to a C-string and dynamically allocate just enough memory to store all the characters entered by the user PLUS the ‘\0’ char than must be appended at the end of the C-string. 

     userInput[ptrLength] = '\0';   

     cout << endl; 
     myVowels(userInput, ptrLength); 
     cout << endl << endl; 
     //delete [] userInput; //deleting here breaks the program. Not sure why right now. 
     //userInput = nullptr; 
     //5. The user must be asked if he/she wants to continue entering values or quit. 
     cout << endl << "To enter another phrase press Y. To exit press any key." << endl; 
     cin >> cont; 
     cin.clear(); 
     cin.ignore(256, '\n'); 
    }//end while cont = Y 

    delete userInput; 
    userInput = nullptr; 
    system("pause"); 
    return 0; 
} 

更新的代碼:

int main(){ 

    char cont = 'y'; 
    char *userInput = nullptr; 

    while (cont == 'y' || cont == 'Y') 
    { 

     int num = 10; 
     int ptrLength = num; 
     userInput = new char[ptrLength]; 
     char *temp = nullptr; 

     cout << "Please enter a word or phrase: ";//2. Asks the user to enter any string (any sequence of characters) 
//FIX I FOUND, BUT IT DOES NOT WORK AT ALL 
     while (cin.peek() != '\n'){ 
      cin >> userInput[ptrLength]; 
      if (ptrLength = num){ 
       num *= num; 
       temp = new char[num]; 
       for (int i = 0; i < num/2; i++) 
       { 
        temp[i] = userInput[i]; 
       } 
       delete [] userInput; 
       userInput = temp; 
       delete [] temp; 
      } 
     } 
     //1. You must use a pointer to a C-string and dynamically allocate just enough memory to store all the characters entered by the user PLUS the ‘\0’ char than must be appended at the end of the C-string. 

     userInput[ptrLength] = '\0';   

     cout << endl; 
     myVowels(userInput, ptrLength); 
     cout << endl << endl; 

     //userInput = nullptr; 
     //delete [] userInput; //This works, but by switching to nullptr I am not deleting the memory allocated. If I just have the delete with or without [] the program breaks. Tried with user input declared inside and outside of the WHILE statement. Heap is being corrupted. 

     //5. The user must be asked if he/she wants to continue entering values or quit. 
     cout << endl << "To enter another phrase press Y. To exit press any key." << endl; 
     cin >> cont; 
     cin.clear(); 
     cin.ignore(256, '\n'); 
    }//end while cont = Y 

    delete userInput; 
    userInput = nullptr; 
    system("pause"); 
    return 0; 
} 

我知道堆被損壞,我從我改變了分配內存的方式懷疑。我不確定爲什麼修復程序無法正常工作,所以這是所有的教程。

+0

'(X^2)/ 2 = x'和'(ptrLength = NUM​​)=(ptrLength == NUM​​)'!。並且你釋放你的原始和新的字符緩衝區。也許橡皮鴨會幫助。 –

+0

@JamesRoot'(x^2)!=(x * x)'in C++ – MikeCAT

回答

3

下面是我想象與your rubber duck您的通話將走在這一點上:

int num = 10; 
    int ptrLength = num; 
    userInput = new char[ptrLength]; 

你(說你的橡皮鴨子):好了,上面有哪些歸結爲 的是,我被分配10個字符的緩衝區。 userInput這裏 指向十個字符,userInput[0]userInput[9]

橡皮鴨:好的。

你說:這兩個numptrLength被設置爲10

橡膠鴨的價值:對我來說很有意義。

while (cin.peek() != '\n'){ 
     cin >> userInput[ptrLength]; 

您:那麼,我是否讀下一個字符是一個換行符,如果沒有,我 放在userInput[ptrLength]

橡膠鴨輸入:等等,什麼是初始值ptrLength

你:10,正如我剛纔所說的那樣。

橡膠鴨:但你不是剛說,你只有userInput[0] 通過userInput[9],分配給您的緩衝區,並寫入 東西userInput[10],在這一點上,會破壞堆。

那麼,你在這裏對你的橡皮鴨的問題有什麼回答?

1
  • userInput[ptrLength]超出範圍,不得在userInput = new char[ptrLength];之後訪問。
  • 條件ptrLength = num不是一個平等測試,但一個任務,我想這不是你想要的。
  • 您忘了在閱讀後更新ptrLength
  • 您刪除了新分配的緩衝區,並使其不可用。
  • 您應該刪除通過new創建的任何內容。
  • 必須使用delete之前 assgning nullptr。對於通過new[]分配的內容,還可以使用delete[]
  • 經過num = num*num;,num/2一般不會在先num。你必須計算平方根,從新的num獲得oid num

校正代碼:

#include <iostream> 
#include <cstdlib> 
using std::cout; 
using std::cin; 
using std::endl; 

void myVowels(const char *userInput, int ptrLength){ 
    cout << "myVowels(" << userInput << ", " << ptrLength << ")\n"; 
} 

int main(){ 

    char cont = 'y'; 
    char *userInput = nullptr; 

    while (cont == 'y' || cont == 'Y') 
    { 

     int num = 10; 
     int ptrLength = 0; 
     userInput = new char[num]; 
     char *temp = nullptr; 

     cout << "Please enter a word or phrase: ";//2. Asks the user to enter any string (any sequence of characters) 
     while (cin.peek() != '\n'){ 
      cin >> userInput[ptrLength++]; 
      if (ptrLength == num){ 
       int oldNum = num; 
       num *= num; 
       temp = new char[num]; 
       for (int i = 0; i < oldNum; i++) 
       { 
        temp[i] = userInput[i]; 
       } 
       delete [] userInput; 
       userInput = temp; 
      } 
     } 
     //1. You must use a pointer to a C-string and dynamically allocate just enough memory 
     // to store all the characters entered by the user PLUS the ‘\0’ char than must be 
     // appended at the end of the C-string. 

     userInput[ptrLength] = '\0'; 

     cout << endl; 
     myVowels(userInput, ptrLength); 
     cout << endl << endl; 

     delete [] userInput; 
     userInput = nullptr; 

     //5. The user must be asked if he/she wants to continue entering values or quit. 
     cout << endl << "To enter another phrase press Y. To exit press any key." << endl; 
     cin >> cont; 
     cin.clear(); 
     cin.ignore(256, '\n'); 
    }//end while cont = Y 

    system("pause"); 
    return 0; 
} 
+0

謝謝。 Duck Conversation幫助我們指出了我的方式中的其他錯誤。我能夠專注並使代碼工作超越可擦除性。真的很感謝評論。 – new2Me