C++代码永远运行并消耗内存
C++ code runs forever and eats memory
它似乎在MakeData函数中,因为这是破坏执行的原因。我非常不确定为什么这不起作用,因为我的老师和我的许多同学的执行力几乎相同,这很好。我知道一个事实,这个代码的几乎相同的版本,带有 windows 文件名,也不能在 Windows 上运行。我已经编译了代码。我运行了调试器。什么都没有出现。我运行的调试器刚刚运行代码,直到出现非常晦涩的错误,或者它本质上表明该过程处于某种无限循环中。任何帮助将不胜感激!
/*
*Program Description:A program to sort a series of strings and scores from a file.
*
*Programmer:Timothy A. Gass
*Date:01/17/17
*/
#include <iostream>
#include <string>
#include <math.h>
#include <fstream>
#include <vector>
#include <ctime>
using namespace std;
void makeData(string);
void getData(vector<string> &, vector<int> &, string);
int main(){
srand(time(0));
string fname = "/home/tim/dev/c++/chpt9/data.txt";
vector<string> name;
vector<int> score;
makeData(fname);
getData(name, score, fname);
for(int i = 0; i < score.size(); i++){
cout << score[i] << endl;
cout << name[i] << endl;
}
cout << "Press enter to exit." << endl;
cin.ignore();
cin.get();
return 0;
}
void makeData(string fname){
int rand1, rand2, rand3;
const int SCORE_MAX_SIZE = 100;
ofstream make(fname);
const int PEOPLE_NUM = 50;
vector<string> firstNames = {
"Gus",
"Taneka",
"Shane",
"Rosella",
"Bennett",
"Filiberto",
"Khadijah",
"Mafalda",
"Rusty",
"Janiece",
"Shavonne",
"Azalee",
"Enedina",
"Heidy",
"Lavelle",
"Darleen",
"Ashton",
"Glynis",
"Gale",
"Norene",
"Madaline",
"Elvin",
"Jacqueline",
"Kristofer",
"Zachary",
"Lorretta",
"Jim",
"Shanelle",
"Tonja",
"Alethia",
"Kasha",
"Katheleen",
"Joyce",
"Kirstin",
"Neil",
"Belkis",
"Maisha",
"Doretha",
"Eliseo",
"Rhiannon",
"Annamarie",
"Latoria",
"Jerica",
"Betsey",
"Delinda",
"Pamula",
"Porsha",
"Fredia",
"Wilda",
"Belen"
};
vector<string> lastNames = {
"Best",
"Shields",
"Finley",
"Blankenship",
"Hobbs",
"Nichols",
"Mcneil",
"Robles",
"Moyer",
"Hays",
"Elliott",
"Ruiz",
"Ritter",
"Gamble",
"Zamora",
"Cole",
"Larson",
"Ibarra",
"Choi",
"Santana",
"Gray",
"Crane",
"Campos",
"Wright",
"Morris",
"Flores",
"Newman",
"Santos",
"Li",
"Archer",
"Chavez",
"Avery",
"Mora",
"Liu",
"Lutz",
"Miles",
"Stewart",
"Austin",
"Wu",
"Turner",
"Brennan",
"Ferrell",
"Mcmillan",
"Whitney",
"Odonnell",
"Conley",
"Maxwell",
"Stafford",
"Carlson",
"Peck"
};
for(int i = 0; i < PEOPLE_NUM; i++){
rand1 = rand()%50;
rand2 = rand()%50;
rand3 = rand()%(SCORE_MAX_SIZE+1);
make << firstNames.at(rand1) + " " + lastNames.at(rand2) << endl;
make << rand3 << endl;
}
}
void getData(vector<string> &name, vector<int> &score, string fname){
ifstream get(fname);
string str;
int num;
if(get.fail()){
cout << "File could not be opened!" << endl;
}
else
{
while(!get.eof())
{
getline(get, str);
get >> num;
cin.ignore();
name.push_back(str);
score.push_back(num);
}
}
}
黄欣的评论是正确的。事实证明,使用getline和cin导致了某种形式的无限循环,它会吞噬内存,直到计算机最终崩溃。我仍然不知道为什么没有解决方案,或者为什么一起使用 cin 和 getline 会产生如此可怕的后果,特别是考虑到真的没有错误代码。即便如此,在getData函数中用getline替换cin,然后转换回整数会产生一个干净的程序。
相关文章:
- 有没有一种方法可以测量c++程序的运行时内存使用情况
- 在运行时为动态分配的内存输入值
- 如何运行外部程序,向其传递内存地址以读取/写入?
- 如何配置bazel以运行地址/内存清理器?
- 为什么我的C 代码在Linux上运行,而在Windows上不运行,是否有RAM内存的问题
- Visual C 运行时:Malloc将指针返回到已经使用的内存(包含实际字符串)
- C 获得运行时间和内存使用量
- cv:内存位置的异常 - 运行 openCV 代码 Visual Studio 2013 & Visual Studio 2017 - Windows 7
- malloc - 运行时内存指针类型分配
- PostgreSQL在DB服务器中消耗更多内存,以进行长期运行的连接
- 如何在以下C程序中计算运行时间内存和最大数据存储器使用情况
- 降低CUDA内核运行时:内核中矩阵的动态内存分配
- 运行实验的好方法是C 中算法的内存使用情况
- 在多次运行C++程序时将巨大的矩阵保存在内存中
- C++ 运行少量数字时超出内存限制
- 为什么在 v8 中运行嵌入在 c++ 中的非常简单的脚本会占用内存?
- 在动态内存分配中出现运行时错误
- Windows是否支持从内存运行程序
- 如何正确管理内存(运行时)C++
- 内存运行时错误:我们在哪里删除这个指针