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

-1

When the user input is exactly 23, I get this error thrown. It's some kind of heap corruption, but I have no idea how to fix it. It's just a simple homework assignment whereby it creates a dynamically allocated character array, the length of which is determined by the user. It then displays the array, sorts in alphabetical order, and then displays it.

Program:

#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;
}

You can see that my solution has been commented out. If I check for the number 23, the program works perfectly. I'm also using Eclipse IDE on a Razer Blade R2 with an i7-3632QM, 8gb RAM, 64bit Windows 8.1, if that makes a difference.

EDIT: I should mention that the purpose of the assignment was to use array pointers. So, using the size of the array to determine when the for loops ends was out of the question. That's why I needed the [N+1] = '\0' in order to determine when the for loops were supposed to finish.

c++
runtime-error
asked on Stack Overflow Jul 13, 2017 by trialAndError • edited Jul 13, 2017 by Javia1492

1 Answer

3

The below is a problem:

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

if you allocate N+1 the index goes from 0 to N, accessing N+1 is heap corruption

answered on Stack Overflow Jul 13, 2017 by IlBeldus

User contributions licensed under CC BY-SA 3.0