1 Replies - 897 Views - Last Post: 11 September 2016 - 11:16 AM

#1 cyphersmith  Icon User is offline

  • New D.I.C Head

Reputation: 4
  • View blog
  • Posts: 17
  • Joined: 23-July 16

term called after throwing and instance of std::bad_alloc

Posted 24 July 2016 - 03:40 PM

first attempt at a vector. Trying to write out a 2 col csv file to vector. Modified my own working array code to use as vector. Don't think it is the same? Compiled fine but on run I got a "terminate called after throwing an instance of "std::bad_alloc" what(): std::bad_alloc. application term in unusual way yada yada. Please help and explain why this doesn't work and how to fix it. I'll dance at your wedding!!!


const int BUFFSIZE = 80; // at top just placed to show

int balance(){
    vector< vector<double> > vData; // Define a vector of vectors
    vector<double> vTemp;
    char buff[BUFFSIZE];

    ifstream accFile;
    stringstream ss;

    accFile.open( ACCOUNT_FILE );
        if ( !accFile ){
            cout << "Error: unable to open file" << endl;
        }else{
            while( !accFile.eof() ){
                for( int i = 0; i < accFile.eof(); i++ ){
                    accFile.getline( buff, BUFFSIZE );
                    ss << buff;
                    for( int j = 0; j < 2; j++ ){
                        ss.getline( buff, 8,',' );
                        vTemp.push_back( atof( buff ) );
                        }
                        ss << "";
                        ss.clear();
                    }
;                vData.push_back( vTemp );
                }

        }

    accFile.close();

    for ( int i = 0; i < vData.size(); i++ ){
        for( int j = 0; j < vData[i].size(); j++ ){
            cout << vData[i][j];
            }
        cout << endl;
        }
}


Is This A Good Question/Topic? 0
  • +

Replies To: term called after throwing and instance of std::bad_alloc

#2 Thomas1965  Icon User is offline

  • D.I.C Head

Reputation: 52
  • View blog
  • Posts: 206
  • Joined: 09-September 16

Re: term called after throwing and instance of std::bad_alloc

Posted 11 September 2016 - 11:16 AM

A bad_alloc is thrown by operator new when the system runs out of memory. Not sure where it comes from, but the way you read the file looks a bit strange. How big is the file?
Try the following code to see if you still get this exception.
BTW. Why do you use nested vectors - a vector of structs would be easier to handle and also would use less memory.

int balance()
{
    vector<vector<double>> vData; // Define a vector of vectors
    vector<double> vTemp;
    char buff[BUFFSIZE];

    stringstream ss;

    ifstream accFile(ACCOUNT_FILE);
    if (!accFile)
    {
       cout << "Error: unable to open file" << endl;
    }
    else
    {
      while (accFile.getline (buff, BUFFSIZE))
      {
         ss << buff;
         for( int j = 0; j < 2; j++ )
         {
           ss.getline( buff, 8,',' );
           vTemp.push_back( atof( buff ) );
         }
         ss << "";
         ss.clear();
      }
      vData.push_back( vTemp );
   }
   for (size_t i = 0; i < vData.size(); i++ )
   {
     for( size_t j = 0; j < vData[i].size(); j++ )
     {
       cout << vData[i][j];
     }
     cout << endl;
   }
   // here you need to return sth.
   return 0;
}


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1