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 8084697
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T17:52:26+00:00 2026-06-05T17:52:26+00:00

Today, on EFNet C++ Wiki in article heap corruption , I found two pieces

  • 0

Today, on EFNet C++ Wiki in article heap corruption, I found two pieces of code.

void this_is_bad() /* You wouldn't believe how often this kind of code can be found */    
{    
    char *p = new char[5];    /* spend some cycles in the memory manager */    
    /* do some stuff with p */    
    delete[] p;      /* spend some more cycles, and create an opportunity for a leak */    
 }  

Alternate way:

void this_is_good()    
{    
   /* Avoid allocation of small temporary objects on the heap*/   
   char p[5];    /* Use the stack instead */   
   /* do some stuff */  
}    

Can someone help me in understanding why the first piece of code is not considered good?

  • 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-05T17:52:28+00:00Added an answer on June 5, 2026 at 5:52 pm

    When using char* p, you’re allocating p on the heap so you have to take care of deleting it at the end. Between the char *p and delete, in do some stuff with p, the code could throw an exception and p is leaked.

    When using char p[5], you’re allocating p on the stack that way you don’t have to take care of delete and even if the code throws an exception, you’re safe.

    void this_is_bad()   
    {    
      char *p = new char[5]; //on the heap
      // What happens if I throw an unhandled exception here?
      delete[] p;  // I never get to delete p and it gets leaked
    }  
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Today while I was writing some code for two methods that shows and hides
Today, while I was randomly reading the JavaScript patterns O'Reilly book, I found one
Today i tested my website on seo tool site and found following error: Your
Today while inside a client's production system, I found a SQL Server query that
Today I was writing some C code to sort an array of structs using
Today's problem in my code is kind of strange, and I could not reproduce
Today I had a strange encounter with gcc. consider the following code: float len[ELEM+1];
Today I was reading code from some very popular numerical libraries written in FORTRAN
Today I was happily coding away when I got to a piece of code
Today I had a coworker suggest I refactor my code to use a label

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.