[英]std::bad_alloc when storing single characters from a text file into a vector C++
因此,我的目標是從文件中讀取並存儲單個字符(它們是基因組序列)。 文件很大,基本上看起來像這樣:
>gi|188033402|emb|CU859091.1| A BAC library has been constructed from PN40024...
AGCTCCTTTTTAAAACAACATTCAAGAAATTGGAGTTTGGCATTTTTACAAAGAGCATATTCAGCTCTGG
GAAGTATTTCTTGAGCAAAGAGGAGCGAAAATAGATCCTAAATGTTCCCCAGATTTTCCCCAGATTCCAA
以“>”字符開頭的行可以完全忽略。 所以基本上我只需要“ AGCTCCTTTTTA ...”部分。
我的代碼適用於較小的文件,但是對於較大的文件會崩潰。 我試圖找出這里的內存問題以及如何解決它。 運行文件時出現的錯誤是:
terminate called after throwing an instance of 'std::bad_alloc'
what(): std::bad_alloc
Aborted (core dumped)
這是我的簡化代碼,用於讀取單個字符並將其存儲到向量中:
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);
}
}
}
我一直在尋找關於stackoverflow的其他“ std :: bad_alloc”帖子,但似乎找不到幫助我解決問題的帖子,因此,我們將不勝感激! 如果您不知道,我還是編程新手,不勝感激。
非常感謝你!
這意味着程序最終將請求連續的內存塊,該內存塊應足夠大,以致系統無法提供。
如果您堅持使用該方法,則快速解決方法可能是使用deque
而不是vector
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.