抛出'std :: bad_alloc''实例后,'terminate调用了什么意思?

问题描述 投票:0回答:1

我正在编写一个打开文本文件并读取每一行的函数,然后将其添加到字符串向量中(每行只是一个单词)。该代码可以编译,但是在尝试运行它时会出现错误。

这是调用函数并尝试打印矢量后出现的错误:

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

如果我调用该函数但不打印矢量,则会得到Segmentation Fault

我在网上查找了此错误,但找不到有关此错误的确切定义。从我所看到的来看,似乎与使用过多内存有关引发了此错误。也许我的代码中有什么会引起无限循环?该错误的确切含义是什么,它将如何应用于我编写的代码?

 vector<string> readToVector(string fileTo) {
        vector<string> setVector;
        string temp;
        ifstream openSet(fileTo.c_str());
        if (openSet.is_open()) {
            while ( getline (openSet,temp) ) {
                    setVector.push_back(temp);
            }
        }
        else {
            cout << "Unable to open set file." << endl;
        }
    }
function memory vector memory-leaks ifstream
1个回答
0
投票

没关系,我很傻。我只是从未在函数中返回向量。 facepalm

© www.soinside.com 2019 - 2024. All rights reserved.