bad alloc error in c++ of linux

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • bhawna popli
    New Member
    • Sep 2012
    • 1

    bad alloc error in c++ of linux

    I got bad alloc error and due to this i got segmentation fault. but if i again execute my program then im not get this problem again.Is this problem realy dangerous and what is the solutionfor that problem.
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    Generally you get std::bad_alloc when your program is unable to allocate the amount of memory requested in a call to new or new[].

    This suggests a design fault in you code because on Linux you would normally expect to have a fairly large amount of memory backed by a swap disk, for example on my system I have 2 Gbyte ram backed by a 2Gbyte swap disk.

    Either your program is allocating a large number of medium size chunks of memory or it is trying to allocate 1 huge chunk of memory.

    Either way your design is flawed since the system wont/can't do what you have requested.

    SIGSEGV (segmentation fault) is then the error produced when your program attempts to access an invalid address such as is produced by not initialising a pointer correctly because new has thrown std::bad_alloc.

    You need to
    1. Alter your program so that it correctly handles failure to allocate memory in a graceful manor, not just through SIGSEGV and crash out even if that manor is catch the error and exit gracefully by calling abort()
    2. Alter your program design so that it handles its memory better and can do its job without running into failure to allocate memory.


    I have only ever managed to produce a std::bad_alloc error when I have specifically written a program to continuously allocate memory until it is produced.

    Comment

    Working...