I am getting this error when running a very similar code like the
I have memory problem,but i dont know how can solve it.
please help.

Thanks in advance.


terminate called after throwing an instance of 'std::bad_alloc'
what(): std::bad_alloc


int main(){
  /** Declare classes **/
  for(long int i = 0 ; i < 50000 ; i++){
    class Rahim   *rahim  = new Rahim( (unsigned)(i));
    class Bahram  *bahram = new Bahram;
    class Asgar   *asgar  = new Asgar;
    /*****Body of the code******/   
    throw( asgar, bahram , Rahim );
    delete (bahram);
    delete (Rahim);
    delete (asgar);
      }//end for
      return 0;
  void throw(class Asgar *asgar, class Bbahram  *bahram ,class Rahim *Raahim ){
 /** Declare variables **/
  long int time; 
  long int step = 1999;
  /**     Time Loop         **/
  for( time = 0 ;time < GT ;time++ ){ 
    get_bahram( bahram, Rahim );
    asgar -> get_coordinates ( bahram , time );
    if(step == time){
      long int time_Rg = (step + 1);
      asgar -> calculate_r  ( time_Rg );
      step += 2000;
    }//end if
 } //end for
} //end function 


Recommended Answers

All 2 Replies

On line 7 you have the body of the code. What's in there? std::bad_alloc is thrown when no memory is available for the new operation. Can the loop continue without getting to the part when delete is called? If so, you may be running out of memory. Check the system monitor to see if the memory usage increases sharply while your code is running.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.