Joey Arnold Andres
Joey Arnold Andres

Reputation: 368

C++ memory deallocation sequence causing error

I was reading a book a long time ago and told me the pattern how to allocate and deallocate memory for pointers. For some reason it is not working in mine right now and previous project that i had. Only now that I decided to fix this allocation problem. So here is the code that have the problem:

    // Encrypting or Decrypting Mode
    bool ModeSelected(false);                   // stores the result if the user had selected a valid mode
    bool Mode(ENCRYPT);                         // saves the Mode if Encrypt or Decrypt

    while(!ModeSelected)
    {
        cout << "Enter E to Encrypt or D to Decrypt: " << endl;
        char resultED[MAXCHAR];                 // stores the result if to Encrypt or Decrypt
        char *presultED(nullptr);               // stores the result
        cin.getline(resultED, MAXCHAR, '\n');   
        presultED = new char((strlen(resultED)) + 1);
        strcpy(presultED, resultED);                // copy the result the char pointer variable

        if((*(presultED + 0) == 'E') || 
           (*(presultED + 0) == 'e'))                       // The user wants Encrypt
        {
            cout << endl << "Encrypt Mode is Selected" << endl;
            Mode = ENCRYPT;
            ModeSelected = true;
        }
        else if ((*(presultED + 0) == 'D') || 
                 (*(presultED + 0) == 'd'))             // The user wants Decrypt
        {
            cout << endl << "Decrypt Mode is Selected" << endl;
            Mode = DECRYPT;
            ModeSelected = true;
        }
        else
        {
            // Input is invalid
            cout << endl << "Input is invalid" << endl;
        }
        // clean up
        if(presultED)                           // If not null then delete it
        {
            // Garbage collection
            // Contact Stack Overflow
                 delete[] presultED;
                 presultED = nullptr;
        }
    }

You see the // clean up section of the code is exactly how the book told me how to deallocate memory. I also kinda understand the computer science behind that. Now pls tell me the problem. Thank you.

Upvotes: 0

Views: 189

Answers (2)

Tony The Lion
Tony The Lion

Reputation: 63250

Problem is that you're not allocating an array of char but you are deleting array of char by calling delete[]

You should allocate

presultED = new char[(strlen(resultED)) + 1];

like this, because that creates an array of chars.

You cannot delete an array if none exists.

Upvotes: 1

Luchian Grigore
Luchian Grigore

Reputation: 258618

You allocate presultED with new and release it with delete[], which yields undefined behavior.

presultED = new char((strlen(resultED)) + 1);  //allocates one char

is not the same as

presultED = new char[(strlen(resultED)) + 1];  //allocates an array

There may be some other errors, I just stopped reading once I saw this :)

Upvotes: 1

Related Questions