我正在尝试运行下面的C++代码,我得到了这个错误:谁能帮我澄清为什么这是问题输入: get /text_4.txt 9
终止调用后抛出' std::bad_alloc‘的什么():std::bad_alloc中止(内核转储)在读取几个类似的线程后,解决方案是检查动态内存分配。但是,我的代码没有任何动态分配的内存。
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <sys/types.h>
#include <sys/stat.h>
using namespace std;
vector<string> arrangefile(vector<string>& scale, int width, int &number) {
int beginning = 0; int total = 0;
vector<string> result;
for(int i = 0; i < scale.size(); i++)
{
total += scale[i].size(); // add length of each word
if(total + i - beginning > width) // checking if the value has exceeded the maximum width
{
total -= scale[i].size();
string sentence= "",low="";
int last = i-1;
int space = width - total; // calculate number of spaces in each line
int check = max(last-beginning, 1);
int even = space/check;
while(even--){
low += " ";
}
int mod = space%check;
for(int j = beginning; j <= last; j++)
{
sentence += scale[j]; //find all values in a sentence
if(j < last || beginning == last)
sentence += low; // add the word low to the larger sentence
if(j - beginning < mod)
sentence += " ";
}
result.push_back(sentence); // add the sentence to the vector
number++; // counts the number of sentences
beginning = i;
total = scale[i].size();
}
}
string sentence =""; // for the last line
int last = scale.size()-1;
int check = last-beginning;
int space = width - total - check;
string low="";
while(space--){
low += " ";
}
for(int j = beginning; j <= last; j++)
{
sentence += scale[j];
if(j < last){
sentence += " ";
}
}
sentence += low;
result.push_back(sentence); // // add the sentence to the vector
number++; // counts the number of sentences
return result;
}
int main(){
string filepath, word;
int M, number=0;
cin >> filepath;
cin >> M;
ifstream fin;
fin.open(filepath.c_str());
unsigned found = filepath.find_last_of("/");
string b = filepath.substr(found+1);
int create = b.size();
string between = b.substr(0, create-4);
string final = between + "_formatted.txt";
string ending = "output/" + final;
mkdir ("output", 0777);
ofstream fout;
fout.open(ending);
for(int i = 0, count = 0; i<M; i++, count ++){
if(count == 9){
fout<<count;
count = -1;
}
else
fout<<count;
}
fout<<endl;
vector <string> first;
vector <string> second;
while(fin >> word){
first.push_back(word);
}
if(first.empty()){
cout<<"0 formatted lines written to "<< ending<<endl;
}
else{
second = arrangefile(first, M,number);
for (auto i = second.begin(); i != second.end(); ++i)
fout << *i <<endl;
cout<<number<<" formatted lines written to "<<ending<<endl;
}
fin.close();
fout.close();
return 0;
}输入文件text_4.txt:
这是因为在波德莱尔三个年轻人的生活中,并没有发生太多快乐的事情。
输入:输入/text_4.txt 8
发布于 2020-04-18 15:26:34
当我在i==16循环的外部循环的arrangefile中运行您的代码时,我们使用check==1获得width==8和total==10。因此,even被初始化为-2,因此while(even--)循环(几乎)是无限的。因此,它试图向low添加空格,直到内存用完为止。
(请注意,std::string使用的内存是动态分配的,因此您的代码确实有动态内存分配。)std::vector也是如此。)
我还没有对您的算法进行足够深入的分析,以找出正确的修复方法,但可能您的循环应该是while(even-- > 0)。
我将在注释中第二个提示使用您的调试器,并重新发布链接:What is a debugger and how can it help me diagnose problems?。我就是这样找到这个虫子的。
我在调试器gdb下运行了这个程序。它运行了几秒钟,这时我开始怀疑,因为程序似乎没有做任何足够复杂的事情来花费那么多的计算时间。所以我中断了这个程序(Ctrl),让我看看它在哪里,它在做什么。我可以看到它在while(even--)循环中。这也令人怀疑,因为这个循环应该很快完成。因此,我检查了even的值(使用命令p even),发现它是一个很大的负数。这种情况只有从负数开始才会发生,这在逻辑上只有当total大于width时才会发生。对他们的价值观进行考察,我可以看出情况确实如此。
也许,随着您了解更多有关使用调试器的知识,这将是有帮助的。
https://stackoverflow.com/questions/61291154
复制相似问题