2 Replies - 3079 Views - Last Post: 30 September 2011 - 01:21 PM Rate Topic: -----

#1 bassoon15   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 35
  • Joined: 03-May 11

std::bad_alloc error and trouble using gdb to find error c++

Posted 30 September 2011 - 11:29 AM

Hello all!

I am currently programming in c++ to build a program using derived classes. virutal input and output functions are also used. The idea is to make different animal homes. My parent class is Animal_Homes and my current child class is Cat_House.

As of now I am given a runtime error. It is presented directly after typing g++ *.cc and a.out. After these commands are entered, the program "sits" without showing any output, and then the compiler says:

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

So I then decided to try using a makefile to compile, hopefully giving me more hints. It offered more direction:

g++ -g -o animalhouse hw2main.cc
/var/tmp//ccXa4WuE.o: In function `main':
/home/emckever/240C/homework2/hw2main.cc:42: undefined reference to `Cat_House::Cat_House()'
/home/emckever/240C/homework2/hw2main.cc:56: undefined reference to `Cat_House::Cat_House()'
collect2: ld returned 1 exit status


After this step, I try to run gdb to step through and see specifically what is happening:

Reading symbols from /home/emckever/240C/homework2/a.out...done.
(gdb) run
Starting program: /home/emckever/240C/homework2/a.out
[Thread debugging using libthread_db enabled]
where
terminate called after throwing an instance of 'std::bad_alloc'
  what():  std::bad_alloc
[New Thread 1 (LWP 1)]

Program received signal SIGABRT, Aborted.
[Switching to Thread 1 (LWP 1)]
0xfedbb297 in _lwp_kill () from /lib/libc.so.1
(gdb) where
#0  0xfedbb297 in _lwp_kill () from /lib/libc.so.1
#1  0xfedb6a14 in thr_kill () from /lib/libc.so.1
#2  0xfed62d7b in raise () from /lib/libc.so.1
#3  0xfed42171 in abort () from /lib/libc.so.1
#4  0xfef72425 in __gnu_cxx::__verbose_terminate_handler ()
    at /usr/local/src/gnu/gcc/gcc-4.5.2/libstdc++-v3/libsupc++/vterminate.cc:93
#5  0xfef6f295 in __cxxabiv1::__terminate (
    handler=0xfef722d0 <__gnu_cxx::__verbose_terminate_handler()>)
    at /usr/local/src/gnu/gcc/gcc-4.5.2/libstdc++-v3/libsupc++/eh_terminate.cc:39
#6  0xfef6f2f2 in std::terminate ()
    at /usr/local/src/gnu/gcc/gcc-4.5.2/libstdc++-v3/libsupc++/eh_terminate.cc:49
#7  0xfef6f461 in __cxxabiv1::__cxa_throw (obj=0xfef9ece0, tinfo=0xfef9a8fc,
    dest=0xfef70950 <std::bad_alloc::~bad_alloc()>)
    at /usr/local/src/gnu/gcc/gcc-4.5.2/libstdc++-v3/libsupc++/eh_throw.cc:83
#8  0xfef70a67 in operator new (sz=44)
    at /usr/local/src/gnu/gcc/gcc-4.5.2/libstdc++-v3/libsupc++/new_op.cc:58
#9  0x080499d5 in main () at hw2main.cc:42





Can anyone tell me where I am going wrong?

Thanks for reading!

Here is the corresponding code:

hw2main.cc
#include <iostream>
#include <cstdlib>
#include <iostream>
#include <string>
#include <stdlib.h>
#include <list>

#include "Cat_House.h"
#include "Animal_Homes.h"
using namespace std;
void menu (int& choice);


int main()
{
int choice;
int number;
/*
list<Animal_Homes*> homes;
Animal_Homes*tmp; //parent ptr
// these three lines for testing - will be part of menu stuff later
tmp = new Cat_House;
tmp->input();
tmp->output();
*/
//read from file loop

list <Animal_Homes*>homes;
//list <Animal_Homes*>::iterator it;
Animal_Homes *tmp;
//istream ins; //character error...? 
//ostream outs; //maybe you dont need this because it is recognized
               //when you call it in the constructor?
ifstream fin;
ifstream fout;


fin.open("Animal_Directory.txt");
fin>>number;
while(!fin.eof()){
if (number == 1){
  tmp = new Cat_House;                            //line 42!
  tmp->input(fin); // problem with reading in: 
  homes.push_back(tmp);
//end read from file cat house
  } //end if statement
} //end while loop  
fin.close();


//menu loop
do{
   menu(choice);
   switch(choice){
   case 1: if (choice == 1){
   tmp = new Cat_House;                         //line 56!
   tmp -> input(cin);  //same read in error
   homes.push_back(tmp);
   break;
   }

// same for addition cases for other animal houses

case 8: if (choice == 8){
   //output all orders
   list<Animal_Homes*>:: iterator it;
   for (it = homes.begin(); it!= homes.end(); ++it)
      (*it)-> output(cout);
    }
     break;
     case 9: if(choice == 9){
    ofstream fout;
    fout.open("Animal_Directory.txt");
   //write all orders to file
   list<Animal_Homes*>::iterator it;
   for (it = homes.begin(); it!= homes.end(); ++it){
      (*it)-> output(fout);
      
    } 
     fout.close();
   }//end case
    break;
    
 default: cout<<"that is not a valid choice. please choose an interger 1 - 7"<<endl;
  }//end menu options
}//end do while
while (choice !=9);


return 0;

}

// imagine a pretty menu fuction in my actual program




Child Class Cat_House.cc
#include <iostream>
#include <cstdlib>
#include <iostream>
#include <string>
#include <stdlib.h>
#include <list>

#include "Cat_House.h"
#include "Animal_Homes.h"

using namespace std;

Cat_House::Cat_House(){
   // set things to zero in here
}


void Cat_House::input (){  // move this to Cat_House.cc
  Animal_Homes::input(); // calling the parent function from the overriding function

  cout<<"number of toys"<<endl;
  cin>>t;
  cout<<"number of windows"<<endl;
  cin>>w;
  cout<<"number of levels"<<endl;
  cin>>l;
}

void Cat_House::output (){
  Animal_Homes :: output();
  cout<<"number of toys"<<endl;
  cout<<t;
  cout<<"number of windows"<<endl;
  cout<<w;
  cout<<"number of levels"<<endl;
  cout<<l;
}

 void Cat_House:: input (istream &ins){
  Animal_Homes input (istream &ins);

//actual commented out input function is here for read from file
}


 void Cat_House::output (ostream &outs){
// actual commented out code is here for write to file


}




Cat_House.h

#include <list>
#include <iostream>
#include <string>
#include <fstream>
#include <cstdlib>
#include <ctype.h>
#include <stdlib.h>

#include "Animal_Homes.h"

using namespace std;

#ifndef CAT
#define CAT

class Cat_House: public Animal_Homes{

public:

  Cat_House();

  void input();
  void output();

   virtual void input (istream &ins);
   virtual void output (ostream &outs);

private:

int length;  // don't put these here if they're going to handled by the parent
int width;
int area;

int t; //toys 
int w; //windows
int l; //levels

bool carpet; //do you want carpet? yes or no. true if yes. 

};

#endif





Thanks again. Please let me know if anything is unclear.

Is This A Good Question/Topic? 0
  • +

Replies To: std::bad_alloc error and trouble using gdb to find error c++

#2 jimblumberg   User is offline

  • member icon

Reputation: 5916
  • View blog
  • Posts: 17,932
  • Joined: 25-December 09

Re: std::bad_alloc error and trouble using gdb to find error c++

Posted 30 September 2011 - 11:58 AM

Can you tell me the purpose of the following snippet? Where are you deleting the memory you allocated?
Animal_Homes *tmp;

while(!fin.eof()){
if (number == 1){
  tmp = new Cat_House; 


What did you enter for number?

How many times does the loop execute before the crash?

Jim
Was This Post Helpful? 0
  • +
  • -

#3 bassoon15   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 35
  • Joined: 03-May 11

Re: std::bad_alloc error and trouble using gdb to find error c++

Posted 30 September 2011 - 01:21 PM

The purpose of that snippet is to read the data from file.
For example, 1 would correspond to the cat class, and maybe 2 would correspond to a dog class.

so given:
1
5
9
2
4
3
8
When the number one was read in, the input function for the cat class would automatically be called, and input 5 and 9 for whatever variables because it can only take two variables.

when it is finished reading in the 5 and 9, it will exit the cat input function, and go back to the file read in loop. seeig that two correspond with the dog class, the dog class input will then be called, storing the 4 and 3 into the respective variables, seeing that the dog class can only take 3 variables (number of bones, toys, size of bed in feet, etc)

hope that helps :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1