473,241 Members | 1,481 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,241 software developers and data experts.

std::bad_alloc



Hi

I am getting this error when running a very similar code like the
below, it is made for illustration only.

thanks

***************************************
**************** error ****************
(gdb) n
terminate called after throwing an instance of 'std::bad_alloc'
what(): St9bad_alloc

Program received signal SIGABRT, Aborted.
0xb7cd17c7 in raise () from /lib/tls/libc.so.6

(gdb) bt
#0 0xb7cfa7c7 in raise () from /lib/tls/libc.so.6
#1 0xb7cfc06b in abort () from /lib/tls/libc.so.6
#2 0xb7eef644 in __gnu_cxx::__verbose_terminate_handler ()
from /usr/lib/libstdc++.so.6
#3 0xb7eed035 in std::set_unexpected () from /usr/lib/libstdc++.so.6
#4 0xb7eed072 in std::terminate () from /usr/lib/libstdc++.so.6
#5 0xb7eed1aa in __cxa_throw () from /usr/lib/libstdc++.so.6
#6 0xb7eed5eb in operator new () from /usr/lib/libstdc++.so.6
#7 0x0804d28b in __gnu_cxx::new_allocator<fxpair>::allocate (
this=0xbff45c7c, __n=27531841)
at /usr/lib/gcc/i486-linux-gnu/4.0.4/../../../../include/c++/4.0.4/ext/new_allocator.h:88
#8 0x0804d2b0 in std::_Vector_base<fxpair, std::allocator<fxpair::_M_allocate (this=0xbff45c7c, __n=27531841)
at /usr/lib/gcc/i486-linux-gnu/4.0.4/../../../../include/c++/4.0.4/bits/stl_vector.h:117
#9 0x0805405a in std::vector<fxpair, std::allocator<fxpair::_M_insert_aux (this=0xbff45c7c, __position={_M_current = 0x805d1ac}, __x=@0xbff45b38)
at /usr/lib/gcc/i486-linux-gnu/4.0.4/../../../../include/c++/4.0.4/bits/vector.tcc:275
#10 0x08054428 in std::vector<fxpair, std::allocator<fxpair::push_back (
this=0xbff45c7c, __x=@0xbff45b38)
at /usr/lib/gcc/i486-linux-gnu/4.0.4/../../../../include/c++/4.0.4/bits/stl_vector.h:610
#11 0x08052cbd in main () at main.cpp:85

***************************************
**************** code *****************
#include <string>
#include <vector>
#include <iostream>
using namespace std;

class acc_holder
{
string name;
double a, b, c, d, e, f, g;
double max_weekly_withdraw;
double daily_withdraw;
int h;

public:
acc_holder(string nam, double d_withdraw)
: name(nam), daily_withdraw(d_withdraw),
a(0),
b(0),
c(0),
d(0),
e(0),
f(0),
g(0)
{}
void update_weekly_figurs()
{
max_weekly_withdraw += daily_withdraw;
cout << name << ": " << max_weekly_withdraw << '\t';
}
};

int main(){

acc_holder jack("jack", 1.1);
acc_holder mary("mary", 1.2);
acc_holder sam("sam", 1.3);
acc_holder job("job", 1.4);
vector<acc_holderfund_participanets;
fund_participanets.push_back(jack);
fund_participanets.push_back(mary);
fund_participanets.push_back(sam);
fund_participanets.push_back(job);

for( unsigned i = 0; i<=3; i++){
for(unsigned j = 0; j<=fund_participanets.size()-1; j++){
fund_participanets[j].update_weekly_figurs();
}
cout << '\n';
}
}

Aug 14 '06 #1
5 55861

Gary Wessle wrote:
Hi

I am getting this error when running a very similar code like the
below, it is made for illustration only.

thanks

***************************************
**************** error ****************
(gdb) n
terminate called after throwing an instance of 'std::bad_alloc'
what(): St9bad_alloc

Program received signal SIGABRT, Aborted.
0xb7cd17c7 in raise () from /lib/tls/libc.so.6

(gdb) bt
#0 0xb7cfa7c7 in raise () from /lib/tls/libc.so.6
#1 0xb7cfc06b in abort () from /lib/tls/libc.so.6
#2 0xb7eef644 in __gnu_cxx::__verbose_terminate_handler ()
from /usr/lib/libstdc++.so.6
#3 0xb7eed035 in std::set_unexpected () from /usr/lib/libstdc++.so.6
#4 0xb7eed072 in std::terminate () from /usr/lib/libstdc++.so.6
#5 0xb7eed1aa in __cxa_throw () from /usr/lib/libstdc++.so.6
#6 0xb7eed5eb in operator new () from /usr/lib/libstdc++.so.6
#7 0x0804d28b in __gnu_cxx::new_allocator<fxpair>::allocate (
this=0xbff45c7c, __n=27531841)
at /usr/lib/gcc/i486-linux-gnu/4.0.4/../../../../include/c++/4.0.4/ext/new_allocator.h:88
#8 0x0804d2b0 in std::_Vector_base<fxpair, std::allocator<fxpair::_M_allocate (this=0xbff45c7c, __n=27531841)
at /usr/lib/gcc/i486-linux-gnu/4.0.4/../../../../include/c++/4.0.4/bits/stl_vector.h:117
#9 0x0805405a in std::vector<fxpair, std::allocator<fxpair::_M_insert_aux (this=0xbff45c7c, __position={_M_current = 0x805d1ac}, __x=@0xbff45b38)
at /usr/lib/gcc/i486-linux-gnu/4.0.4/../../../../include/c++/4.0.4/bits/vector.tcc:275
#10 0x08054428 in std::vector<fxpair, std::allocator<fxpair::push_back (
this=0xbff45c7c, __x=@0xbff45b38)
at /usr/lib/gcc/i486-linux-gnu/4.0.4/../../../../include/c++/4.0.4/bits/stl_vector.h:610
#11 0x08052cbd in main () at main.cpp:85

***************************************
**************** code *****************
#include <string>
#include <vector>
#include <iostream>
using namespace std;

class acc_holder
{
string name;
double a, b, c, d, e, f, g;
double max_weekly_withdraw;
double daily_withdraw;
int h;

public:
acc_holder(string nam, double d_withdraw)
: name(nam), daily_withdraw(d_withdraw),
a(0),
b(0),
c(0),
d(0),
e(0),
f(0),
g(0)
{}
void update_weekly_figurs()
{
max_weekly_withdraw += daily_withdraw;
cout << name << ": " << max_weekly_withdraw << '\t';
}
};

int main(){

acc_holder jack("jack", 1.1);
acc_holder mary("mary", 1.2);
acc_holder sam("sam", 1.3);
acc_holder job("job", 1.4);
vector<acc_holderfund_participanets;
fund_participanets.push_back(jack);
fund_participanets.push_back(mary);
fund_participanets.push_back(sam);
fund_participanets.push_back(job);

for( unsigned i = 0; i<=3; i++){
for(unsigned j = 0; j<=fund_participanets.size()-1; j++){
fund_participanets[j].update_weekly_figurs();
}
cout << '\n';
}
}
The only issue I see is that you don't initialize
max_weekly_withdraw,when I compile & run output looks fine:

martin@jordaan:~$ ./hello
jack: 1.1 mary: 1.2 sam: 1.3 job: 1.4
jack: 2.2 mary: 2.4 sam: 2.6 job: 2.8
jack: 3.3 mary: 3.6 sam: 3.9 job: 4.2
jack: 4.4 mary: 4.8 sam: 5.2 job: 5.6
martin@jordaan:~$

Aug 14 '06 #2
Gary Wessle wrote:
Hi

I am getting this error when running a very similar code like the
below, it is made for illustration only.

thanks

***************************************
**************** error ****************
(gdb) n
terminate called after throwing an instance of 'std::bad_alloc'
what(): St9bad_alloc
std::bad_alloc is an exception that gets thrown by 'new' when it can't
allocate the memory you requested. Something (very likely the standard
library containers you are using) is requesting memory, and that memory
can't be allocated.

You should either change your memory usage patterns so that enough
memory is available to satisfy all the requests, or catch bad_alloc and
handle it in some appropriate way (or preferably, both).

--
Alan Johnson

Aug 14 '06 #3
"Alan Johnson" <aw***@yahoo.comwrites:
Gary Wessle wrote:
>Hi

I am getting this error when running a very similar code like the
below, it is made for illustration only.

thanks

***************************************
**************** error ****************
(gdb) n
terminate called after throwing an instance of 'std::bad_alloc'
what(): St9bad_alloc

std::bad_alloc is an exception that gets thrown by 'new' when it can't
allocate the memory you requested. Something (very likely the standard
library containers you are using) is requesting memory, and that memory
can't be allocated.

You should either change your memory usage patterns so that enough
memory is available to satisfy all the requests, or catch bad_alloc and
handle it in some appropriate way (or preferably, both).

to change the memory usage pattern I did the following which fixed the
problem.
push_back the addresses of the objects into the vector instead of the
objects themselves,

thank you

#include <string>
#include <vector>
#include <iostream>
using namespace std;


class acc_holder
{
string name;
double a, b, c, d, e, f, g;
double max_weekly_withdraw;
double daily_withdraw;
int h;

public:
acc_holder(string nam, double d_withdraw)
: name(nam), daily_withdraw(d_withdraw),
a(0),
b(0),
c(0),
d(0),
e(0),
f(0),
g(0)
{}
void update_weekly_figurs()
{
max_weekly_withdraw += daily_withdraw;
cout << name << ": " << max_weekly_withdraw << '\t';
}
};

int main(){
acc_holder jack("jack", 1.1);
acc_holder mary("mary", 1.2);
acc_holder sam("sam", 1.3);
acc_holder job("job", 1.4);
vector<acc_holder*fund_participanets;
fund_participanets.push_back(&jack);
fund_participanets.push_back(&mary);
fund_participanets.push_back(&sam);
fund_participanets.push_back(&job);

// no acc_holder is permited to do 2 or more consecutive transactions
// therefor they must alternate, each is permited 5 transactions.

for( unsigned i = 0; i<=3; i++){
for(unsigned j = 0; j<=fund_participanets.size()-1; j++){
fund_participanets[j]->update_weekly_figurs();
}
cout << '\n';
}
}

Aug 14 '06 #4
Gary Wessle wrote:
"Alan Johnson" <aw***@yahoo.comwrites:

>>Gary Wessle wrote:
>>>Hi

I am getting this error when running a very similar code like the
below, it is made for illustration only.

thanks

***************************************
**************** error ****************
(gdb) n
terminate called after throwing an instance of 'std::bad_alloc'
what(): St9bad_alloc

std::bad_alloc is an exception that gets thrown by 'new' when it can't
allocate the memory you requested. Something (very likely the standard
library containers you are using) is requesting memory, and that memory
can't be allocated.

You should either change your memory usage patterns so that enough
memory is available to satisfy all the requests, or catch bad_alloc and
handle it in some appropriate way (or preferably, both).

to change the memory usage pattern I did the following which fixed the
problem.
push_back the addresses of the objects into the vector instead of the
objects themselves,

thank you

#include <string>
#include <vector>
#include <iostream>
using namespace std;


class acc_holder
{
string name;
double a, b, c, d, e, f, g;
double max_weekly_withdraw;
double daily_withdraw;
int h;

public:
acc_holder(string nam, double d_withdraw)
: name(nam), daily_withdraw(d_withdraw),
a(0),
b(0),
c(0),
d(0),
e(0),
f(0),
g(0)
{}
void update_weekly_figurs()
{
max_weekly_withdraw += daily_withdraw;
cout << name << ": " << max_weekly_withdraw << '\t';
}
};

int main(){
acc_holder jack("jack", 1.1);
acc_holder mary("mary", 1.2);
acc_holder sam("sam", 1.3);
acc_holder job("job", 1.4);
vector<acc_holder*fund_participanets;
fund_participanets.push_back(&jack);
fund_participanets.push_back(&mary);
fund_participanets.push_back(&sam);
fund_participanets.push_back(&job);

// no acc_holder is permited to do 2 or more consecutive transactions
// therefor they must alternate, each is permited 5 transactions.

for( unsigned i = 0; i<=3; i++){
for(unsigned j = 0; j<=fund_participanets.size()-1; j++){
fund_participanets[j]->update_weekly_figurs();
}
cout << '\n';
}
}
Hi,

Instead, *if* you had a default constructor in acc_holder, you could
have written:

vector<acc_holderfund_participanets;
fund_participanets.push_back(jack);
fund_participanets.push_back(mary);
fund_participanets.push_back(sam);
fund_participanets.push_back(job);

And change -into . in the loop.

The point is in FAQ 8.6:
(http://www.parashift.com/c++-faq-lit...s.html#faq-8.6)
"Use references when you can, and pointers when you have to."
Sincerely,

Peter Jansson
http://www.p-jansson.com/
http://www.jansson.net/
Aug 15 '06 #5

Gary Wessle skrev:
Hi

I am getting this error when running a very similar code like the
below, it is made for illustration only.

thanks

***************************************
**************** error ****************
(gdb) n
terminate called after throwing an instance of 'std::bad_alloc'
what(): St9bad_alloc

Program received signal SIGABRT, Aborted.
0xb7cd17c7 in raise () from /lib/tls/libc.so.6
[snipped stack trace and demo program]

That program looks okay and will NOT throw std::bad_alloc. First rule
is to post a program that contains the behaviour you want to
demonstrate. Since you did not do that, it is impossible to help you.
Most probably you are overwriting memory somewhere in your program. The
"solution" you show in anither post on this thread is not really a
solution, but a rearrangement of code that makes the problem disappear
- for a while!

Kind regards
Peter

Aug 15 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Asfand Yar Qazi | last post by:
Hi, Just wondering, if I do a: std::set_new_handler(std::terminate) I won't have to worry about a std::bad_alloc being thrown when I do a 'new ...' or a 'new(nothrow) ...', right? Its...
24
by: Marcus Kwok | last post by:
Hello, I am working on cleaning up some code that I inherited and was wondering if there is anything wrong with my function. I am fairly proficient in standard C++ but I am pretty new to the .NET...
14
by: Mohsen | last post by:
Hello everyone, In my program, I have to define many pointers and when I want to compile my program (in UNIX), it gives me the following error: terminate called after throwing an instance of...
3
by: schizoid_man | last post by:
Hi, I have the following code snippets and I get a std::bad_alloc error where I think there should be none. I've attached the relevant bits of the base class, derived class and the .cpp file...
4
by: sndive | last post by:
Do linux kernels 2.4 and 2.6 have some stash of memory set aside to handle paging in std::bad_alloc or ... exception handling code when new or new throws that exception? How is that handled? Does...
4
by: daroman | last post by:
Hi Guys, i've problem with my small C++ programm. I've just small template class which represetns a array, everything works fine up to combination with std::string. I did tried it with M$ VC++ and...
4
by: Alerion | last post by:
Hello everyone, I've been a regular of this forum but this is my first post, generally I can find the answer to my question already, but this time I'm having a somewhat specific problem. For...
16
upadhyad
by: upadhyad | last post by:
Hi everyone !! i have an interesting problem...hope you people can help. I am coding in C. the code is posted below: here I have a structure with two character vectors. I have to characters....
4
by: Kaio Vieira | last post by:
Hello, I have a "std::bad_alloc" problem when I try to run a simple C++ program on HP-UX. I don't know if it is a memory limitation or maybe a compiler option that I should use. When my program...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

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.