Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8446205
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T09:52:03+00:00 2026-06-10T09:52:03+00:00

I was reading a book a long time ago and told me the pattern

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T09:52:05+00:00Added an answer on June 10, 2026 at 9:52 am

    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 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am reading some book and I have encountered a piece of code that
I'm learning OpenGL, and I am reading a book to help me a long.
I am reading The thread building block book. I do not understand this piece
I'm reading book about OO programming in JavaScript and get some strange behaviour: function
I am reading a book and it mentions certain data type as being long
phew! That was a long title. I'm reading WROX' book on Professional JavaScript for
I just discovered PHP-ActiveRecord not too long ago after struggling for nearly a month
Recently, I was reading book about Erlang which has hot deployment feature. The deployment
Reading a book I have found the following statement: (Object) Behaviors answer either of
While reading a book about JavaScript I stumbled across an example: var names =

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.