c++pointersstack-corruption

Stack Corruption on Program Termination?


I'm am writing a program for a homework assignment, and can't quite understand what I am doing wrong. The program takes some text input, and outputs it in pig latin, and while the program now achieves this, there is stack corruption on program termination, with VS mentioning "Run-Time Check Failure #2 - Stack around the variable 'token' was corrupted."

I suspect that this is due to going past the boundaries of the pointer somehow, but I don't entirely understand pointers at the current moment, so most things I found don't really make sense, and I'm trying to understand, not just fix what I did wrong.

Attached is the function using the variable that is causing all the trouble (edited to include full program, due to me realizing that some important bits were left out).

int main(void)
{
    char text[] = "";
    char seps[] = " \t\n";
    char *token = NULL;
    char *next_token = NULL;
    bool cont = true;
    bool valid = false;
    char contResp;

    cout << "Enter a sentence to be translated: ";

    do
    {
        cin.getline(text, 200);
        cout << endl << text << endl;
        token = strtok_s(text, seps, &next_token);
        while (token != NULL)
        {
            if (token != NULL)
            {
                printLatinWord(token);
                token = strtok_s(NULL, seps, &next_token);
            }
        }
        cout << endl << endl << "Do you want to enter another sentence (y/n)? ";
        valid = false;
        while (!valid)
        {
            cin >> contResp;
            contResp = tolower(contResp);
            if (contResp == 'y')
            {
                valid = true;
                cin.ignore();
                cout << "Enter a sentence to be translated: ";
            }
            else if (contResp == 'n')
            {
                valid = true;
                cont = false;
            }
            else
            {
                cout << "Invalid response. Please try again.";
                cout << endl << endl << "Do you want to enter another sentence (y/n)? ";
            }
        }
    } 
    while (cont);
    system("pause");
    return 0;
}

void printLatinWord(char *token)
{
    string text = "";
    char *first = token;
    token++;
    while (*token != '\0')
    {
        text += *token;
        token++;
    }
    text += *first;
    text += "ay ";
    cout << text;
}

I am not sure how to get this problem sorted out, but if I could get some help as well as a simple explanation of what I did wrong, I would greatly appreciate it, as pointer arithmetic is mostly gibberish to me.

Thanks in advance!


Solution

  • char text[] = "";
    

    This creates a 1-byte array to hold a '\0' character (NUL terminator). It’s the same as:

    char text[1];
    text[0] = '\0';
    

    cin.getline(text, 200);
    

    This writes up to 200 chars — 199 chars plus the NUL terminator — into the 1-char array.

    Obvious solution: make the array 200 chars long.

    char text[200] = "";
    

    Alternatively, use a std::string for text instead of a char array, and use getline(cin, text); for unlimited line length.