簡體   English   中英

當用戶輸入為23時,C ++中的異常代碼異常,退出值為1,073,740,940(代碼:0xc0000374)

[英]Bizarre exception code in C++, exit value 1,073,740,940 (code: 0xc0000374), when user input is 23

當用戶輸入恰好是23時,我會拋出此錯誤。 這是某種堆損壞 ,但我不知道如何解決。 這只是一個簡單的作業,它會創建一個動態分配的字符數組,其長度由用戶確定。 然后顯示該數組,按字母順序排序,然后顯示它。

程序:

#include<iostream>
#include<cctype>
#include<string>
#include<ctime>

using namespace std;

void showArray(char *array)
{
    for (int count = 0; *(array + count) != '\0'; count++)
        cout << *(array + count) << " ";
    cout << endl;
}

void selectionSort(char *array)
{
    {
       int startScan, minIndex, minValue;

       for (startScan = 0; *(array + startScan) != '\0'; startScan++)
       {
          minIndex = startScan;
          minValue = *(array + startScan);
          for(int index = startScan + 1; *(array + index) != '\0'; index++)
          {
             if (*(array + index) < minValue)
             {
                minValue = *(array + index);
                minIndex = index;
             }
          }
          *(array + minIndex) = *(array + startScan);
          *(array + startScan) = minValue;
       }
    }
}

int main()
{
    int N,
        count;
    string str;
    unsigned int i;
    char min = 97,
         max = 122,
         quit,
         ch;
    srand(time(0));

    while(true)
    {
        char *arrayPtr = nullptr;
        while(true)
        {
            cout << "How many N's do you want?" << endl;
            cin >> str;
            bool truTru = true;

            for (i = 0; i < str.length(); i++)
            {
                char c = str.at(i);
                if ((int)c < 48 || (int)c > 57)
                {
                    cout << "Make sure you are entering numbers, ya dummie." << endl;
                    truTru = false;
                    break;
                }
                /*else if (str == "23")
                {
                    cout << "Sorry, 23 is an invalid input for some reason." << endl;
                    cout << "I need you to enter something diffrent" << endl;
                    truTru = false;
                    break;
                }*/
            }

            if (!truTru)
                continue;
            else
                break;
        }

        N = atoi(str.c_str());
        arrayPtr = new char[N + 1];
        arrayPtr[N + 1] = '\0';

    for(count = 0; count < N; count++)
            arrayPtr[count] = min + (rand() % (max - min));

        showArray(arrayPtr);

        selectionSort(arrayPtr);

        showArray(arrayPtr);

        delete[] arrayPtr;

        cout << "Wanna enter more N's? (1 = no, 2 = yes)" << endl;
        cin >> quit;
            while(true)
            {
                if (quit == '1' || quit == '2')
                        break;
                else
                    {
                        cout << "Incorrect input." << endl;
                        cout << "Try again." << endl;
                        cin >> quit;
                        continue;
                    }
            }
            if (quit == '1')
                break;
            else
            {
                cin.ignore();
                cout << "\nPress ENTER to continue." << endl;
                ch = cin.get();
            }
    }
cout << "Thanks for your N's!" << endl;

return 0;
}

您可以看到我的解決方案已被注釋掉。 如果我檢查數字23,該程序將正常運行。 我還在具有i7-3632QM,8GB RAM,64位Windows 8.1的Razer Blade R2上使用Eclipse IDE,如果有所不同的話。

編輯:我應該提到分配的目的是使用數組指針。 因此,使用數組的大小確定for循環何時結束是不可能的。 這就是為什么我需要[N+1] = '\\0'來確定何時應該完成for循環的原因。

以下是一個問題:

 arrayPtr = new char[N + 1];
 arrayPtr[N + 1] = '\0';

如果您分配N + 1索引從0到N,則訪問N + 1就是堆損壞

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM