简体   繁体   中英

std::bad_alloc when storing single characters from a text file into a vector C++

So my goal is to reading in and store single characters from a file (they're genome sequences). The files are very large and basically look something like this:

>gi|188033402|emb|CU859091.1| A BAC library has been constructed from PN40024...
AGCTCCTTTTTAAAACAACATTCAAGAAATTGGAGTTTGGCATTTTTACAAAGAGCATATTCAGCTCTGG
GAAGTATTTCTTGAGCAAAGAGGAGCGAAAATAGATCCTAAATGTTCCCCAGATTTTCCCCAGATTCCAA

Lines that begin with the " > " character can be completely ignored. So basically I just want the "AGCTCCTTTTTA..." part.

My code works fine for smaller files but crashes with much larger files. I'm trying to figure out what's the memory problem here and how I can solve it. The error I'm getting when I run the file is:

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

Here's my simplified code for reading in the single characters and storing them into a vector:

int main(int argc, char * argv[])
{
   ifstream file (argv[1]);

   vector<char> sequenceA; // to store the single characters
   string line; // to grab lines from the file
   char c; // to grab the single character from the file

   // go through the file
   while(getline(file,line))
   {
      //store the line
      stringstream stream(line);

      // go through and grab each single character in the line
      while(stream.get(c))
      {
         if(c == '>')
         {
            // break and continue to the next line
            break;
         }
         else
         {
            // add the character to the list
            sequenceA.push_back(c);
         }
      }
   }

I was looking through other "std::bad_alloc" posts on stackoverflow, but couldn't seem to find one that helped me solve my issue, so your help would really be appreciated! If you couldn't tell, I'm still pretty new to programming and would appreciate your explanations.

Thank you very much!

This means that the program is eventually asking for a contiguous block of memory large enough that the system can't provide it.

If you insist on your approach, a quick fix would probably be to use deque instead of vector .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM