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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T07:26:59+00:00 2026-05-20T07:26:59+00:00

While allocating memory, the new operator throws an exception if the memory is not

  • 0

While allocating memory, the new operator throws an exception if the memory is not available. On the other hand, malloc returns a NULL. What is the reason for the difference in implementation. Also, on static memory allocation, i.e. on the stack, is there an exception if we run out of memory?

I have already gone through the link What is the difference between new/delete and malloc/free?
but did not get my answer on the difference in implementation of the two

  • 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-05-20T07:26:59+00:00Added an answer on May 20, 2026 at 7:26 am

    The problem with C code is that you are supposed to check the return value of function to make sure they worked correctly. But a lot of code was written that did not check the return value and as a result blew up very nicely when you least expected it.

    In the worst case scenario it does not even crash immediately but continues on corrupting memory crashing at some point miles down stream of the error.

    Thus in C++ exceptions were born.
    Now when there is an error the code does not continue (thus no memory corruption) but unwinds the stack (potentially forcing the application to quit). If you can handle the problem you have to explicitly add code to handle the situation before continuing. Thus you can not accidentally forget to not check the error condition; you either check it or the application will quit.

    The use of new fits this design.
    If you fail to allocate memory then you must explicitly handle the error.
    There is no opportunity to forget to check for the NULL pointer. Thus you can’t go and mess up memory by accidentally using a NULL pointer.

    Also, on static memory allocation, i.e. on the stack, is there an exception if we run out of memory?

    Unfortunately you can not rely on this.
    It is implementation defined what happens on stack-overflow. On a lot of systems it is not even possible to detect the situation resulting in memory corruption and probably ultimately a crash.

    Note

    If you #include <new> then there is a no throw version of new that you can use that returns NULL when there is no memory left. It is best avoided unless have some specialized need.

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

Sidebar

Related Questions

EDIT: Is it possible to NOT use new ? (do not dynamically allocating memory)
I have seen resources show two ways of allocating memory while ensuring that there
Dynamic allocations with new/delete are said to take place on the free-store , while
When using malloc to allocate memory, is it generally quicker to do multiple mallocs
With Linux/GCC/C++, I'd like to record something to stderr whenever malloc/free/new/delete are called. I'm
I'd like to know the total amount of memory allocated while a method runs.
I would like to now how those instructions are allocating memory. For example what
I like to implement linked list in c++ ,while adding new node I dynamically
I'm overloading operator new , but I recently hit a problem with alignment. Basically,
i'm having a weird problem with allocating memory in c++ i'm creating a buffer

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.