我犯了一个低级的C++的逻辑错误
2016-03-22 21:18
459 查看
自己犯了一个低级错误。留作警示。
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <stdlib.h>
#include <time.h>
struct MyNode
{
int num;
std::vector<int> theBeg;
std::vector<int> theEnd;
};
void GenerateFile(std::string fileName)
{// 模拟一个有17W行数据的配置文件。
std::ofstream configFile(fileName);
if (configFile.is_open() == false) return;
srand((int)time(NULL));
for (int i = 0; i < 170000; ++i)
configFile << (rand() % 5 + 3) << std::endl;
}
void ReadFile(std::string fileName)
{// 先将每一行的配置信息转化到Node中,再将Node存储起来。
std::ifstream ifs(fileName, std::ios_base::in | std::ios_base::binary);
if (!ifs) return;
static std::vector<MyNode> theStorage;//存储配置信息的存储器。
MyNode node;//重复使用的结构体。
std::string line;
while (getline(ifs, line))
{
node.num = atoi(line.c_str());
//这里错了,每次重新使用结构体时,应当先清理结构体。这里应当先对vector执行clear操作。
for (int i = 0; i < node.num; ++i)
{
node.theBeg.push_back(node.num);
node.theEnd.push_back(node.num);
}
theStorage.push_back(node);
}
std::cout << theStorage.size() << std::endl;
}
int main()
{
std::string fileName = "D:/config.txt";
GenerateFile(fileName);
ReadFile(fileName);
return 0;
}
//Unhandled exception at 0x74EEC41F in vc_bad_alloc.exe: Microsoft C++ exception: std::bad_alloc at memory location 0x0034EC14.
//我写了一个程序,结果出现了std::bad_alloc异常。原因找了好久,甚至怀疑到了std库了。
//结果发现,是vector内的元素个数猛增,然后耗尽了内存。
//自己犯了一个很低级的逻辑错误。我将自己犯错的代码抽离出来,形成了上面的例子。完。
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <stdlib.h>
#include <time.h>
struct MyNode
{
int num;
std::vector<int> theBeg;
std::vector<int> theEnd;
};
void GenerateFile(std::string fileName)
{// 模拟一个有17W行数据的配置文件。
std::ofstream configFile(fileName);
if (configFile.is_open() == false) return;
srand((int)time(NULL));
for (int i = 0; i < 170000; ++i)
configFile << (rand() % 5 + 3) << std::endl;
}
void ReadFile(std::string fileName)
{// 先将每一行的配置信息转化到Node中,再将Node存储起来。
std::ifstream ifs(fileName, std::ios_base::in | std::ios_base::binary);
if (!ifs) return;
static std::vector<MyNode> theStorage;//存储配置信息的存储器。
MyNode node;//重复使用的结构体。
std::string line;
while (getline(ifs, line))
{
node.num = atoi(line.c_str());
//这里错了,每次重新使用结构体时,应当先清理结构体。这里应当先对vector执行clear操作。
for (int i = 0; i < node.num; ++i)
{
node.theBeg.push_back(node.num);
node.theEnd.push_back(node.num);
}
theStorage.push_back(node);
}
std::cout << theStorage.size() << std::endl;
}
int main()
{
std::string fileName = "D:/config.txt";
GenerateFile(fileName);
ReadFile(fileName);
return 0;
}
//Unhandled exception at 0x74EEC41F in vc_bad_alloc.exe: Microsoft C++ exception: std::bad_alloc at memory location 0x0034EC14.
//我写了一个程序,结果出现了std::bad_alloc异常。原因找了好久,甚至怀疑到了std库了。
//结果发现,是vector内的元素个数猛增,然后耗尽了内存。
//自己犯了一个很低级的逻辑错误。我将自己犯错的代码抽离出来,形成了上面的例子。完。
相关文章推荐
- 查找与排序之冒泡排序篇(C语言实现)
- 代码规范_2:防御编码_c/c++
- C语言指针 直接寻址与间接寻址详解
- C++中实现字符串函数split
- Problem G: C语言习题 医生值班
- C++ 学习之路(10):继承与派生
- C++ 将数据导入到CSV,txt文档中
- C++中的堆和栈的完全解析
- 蓝桥杯 2016c/c++A组 题解
- c++第2次作业
- C++ 链表
- superviseddescent (SDM C++11实现)环境配置
- C++第二次作业
- 结对项目— 词频统计(语言C++)
- C++ static静态成员变量和静态成员函数
- hash --C++
- C语言练习题
- kmp算法c++实现
- C语言中实现BOOL类型
- HDOJ 2024 C语言合法标识符