help-gplusplus
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: std::bad_alloc


From: Earl Purple
Subject: Re: std::bad_alloc
Date: 16 Aug 2006 09:12:28 -0700
User-agent: G2/0.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.


Do you get it with that exact code. There are a couple of issues with
the code you posted,
but I wouldn't expect you to get bad_alloc:

> 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)
>       {}

but you haven't initialised max_weekly_withdraw.

>       for(unsigned j = 0; j<=fund_participanets.size()-1; j++)

This is a dangerous way to loop. If at some point size() happens to be
0 you will
loop indefinitely (or until you get segfault which is more likely)
because -1 is interpreted
as the maximum integer so the condition will always be true.

In this case you have a perfect for_each and mem_fun_ref thus:

std::for_each
  (
     fund_participanets.begin(), fund_participanets.end(),
     mem_fun_ref( &acc_holder::update_weekly_figurs ) 
  );



reply via email to

[Prev in Thread] Current Thread [Next in Thread]