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

  • Home
  • SEARCH
  • 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 7920905
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T16:23:18+00:00 2026-06-03T16:23:18+00:00

why do i get heap corruption when trying to delete the char array. The

  • 0

why do i get heap corruption when trying to delete the char array.

The code below is in “main()”

case 'r':
        char *StrTReverse = new char;
        std::cout << "Enter a string: " << std::endl;
        std::cin  >> StrTReverse;
        std::cout << "String reversed: " << ReverseString(StrTReverse) << std::endl;
        system("pause");
        delete[] StrTReverse; <- Here the is the crash
        break;

below is the ReverseString function

char* ReverseString(char string[])
{

int StringLength = std::strlen(string);
char *NewString = new char[StringLength];

for (int i = 0, j = StringLength-1; i < StringLength;i++,j--)
{
    NewString[i] = string[j];
}
NewString[StringLength] = '\0';
return NewString;

};

I cant understand why this is happening. I am new to c++ so please take that in mind.
thanks for any help 🙂

  • 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-03T16:23:19+00:00Added an answer on June 3, 2026 at 4:23 pm
    char *StrTReverse = new char;
    

    You dont’t allocate an array so dont call delete[] call delete

    Replace

    delete[] StrTReverse;
    

    with

    delete StrTReverse;
    

    The rule is call delete on address returned by new &
    call delete [] on address returned by new [].

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

Sidebar

Related Questions

I get this heap corruption detected message after running this code : uli& uli::operator
I am trying to write a generic Heap Sort algorithm. I get the following
I'm currently attempting to track down the source of heap corruption in our code
Here is my code: std::string readString() { int strLen = Read<int>(); char* rawString =
Hey everyone, I am getting a heap corruption error I cannot figure out. char
I would like to get a reference to all objects in the Java heap,
get an array of bluetooth ID's Broadcast the bluetooth signal manually
I get some doubts about behavior of this code : dispatch_async(queue, ^{ sleep(2); NSLog(@step1);
I get a java.lang.OutOfMemoryError: Java heap space message when reading from an 11g Oracle
Sometimes in execution I get this error message in VS2010 when trying to free

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.