473,387 Members | 1,798 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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.
Sep 20 '12 #1

✓ answered by Banfa

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.

1 2633
Banfa
9,065 Expert Mod 8TB
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.
Sep 20 '12 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

7
by: sNOiSPAMt | last post by:
7.20.3#1 If the size of the space requested is zero, the behavior is implementation-defined: either a null pointer is returned, or the behavior is as if the size were some nonzero value,...
11
by: Vijay Chegu | last post by:
I have built a 64bit ATL COM dll. When i register the dll on IA64 windows Enterprise server 2003, i get following error. DllRegisterServer in mydll.dll failed. Return code was : 0x80020009 ...
13
by: Thomas J. Clancy | last post by:
I was just wondering. After all these years of working on Visual C++, why hasn't Microsoft yet seen fit to fully implement the template portion of the C++ standard when so many other vendors,...
2
by: cnliou | last post by:
Linux 2.4.23 on AMD 450MHz. pgsql 7.4.1 configure --enable-multibyte=UNICODE locale is zh_TW.Big5 IIRC, when I did "initdb -E UNICODE" or "createdb db1", I saw the following message from...
5
by: Janning Vygen | last post by:
Hi, tonight my database got corruppted. before it worked fine. since two days i do the following tasks every night psql -c 'CLUSTER;' $DBNAME psql -c 'VACUUM FULL ANALYZE;' $DBNAME ...
7
by: david wolf | last post by:
I have a problem that is used to test memory leak as follows, however, after each run of the program, if I am using the command "free" on the linux box, I did not see any difference about the...
8
by: Jess | last post by:
Hi, I have a template function that triggered some compiler error. The abridged version of the class and function is: #include<memory> using namespace std; template <class T>
5
by: fimarn | last post by:
I am trying to get rid of compile time error that I am getting only in RHEL5 (not in RHEL4) apparently due to the changes in the stl_list.h file. The error that I am getting is coming from the...
4
by: JDHawk | last post by:
Hi, when I compile my c++ program, MS Visual C++ (2003) shows this error: Damage: after Normal Block (#139) at 0xXXXXXXXX In Debug Mode I found out that it is the deallocation with "delete" in...
18
by: mdh | last post by:
I am not sure if this is directly related to C or the compiler (Xcode) I am using...but I will ask and see. I have started creating unique C files ( .c and .h) so that I can reuse some of the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.