C++ 硬盘和RAM上的文件大小不同

C++ 硬盘和RAM上的文件大小不同,c++,C++,我有36MB的数据文件(文件中的每个值都是双类型)驻留在硬盘上。我的问题是,当我通过C++读取这个文件时,RAM中的内容会被矩阵(由Boost库提供),它只占用36MB的RAM还是不同的?我的内存用完了吗 原因是我在64位ubuntu平台上,内存为8GB,分配错误很严重。同样的文件读取程序也适用于小数据文件 下面是加载(数据真实sim)[.x和y是boost矩阵和向量,分别声明为.h文件中的extern void load_data(const char* filename) { ifst

我有36MB的数据文件(文件中的每个值都是双类型)驻留在硬盘上。我的问题是,当我通过C++读取这个文件时,RAM中的内容会被矩阵(由Boost库提供),它只占用36MB的RAM还是不同的?我的内存用完了吗

原因是我在64位ubuntu平台上,内存为8GB,分配错误很严重。同样的文件读取程序也适用于小数据文件

下面是加载(数据真实sim)[.x和y是boost矩阵和向量,分别声明为.h文件中的
extern

void load_data(const char* filename)
{
   ifstream in(filename);
   string line;
    int line_num = 0;
    if (in.is_open()) {
        while (in.good()) {
            getline(in, line);
            if (line.empty()) continue;
            int cat = 0;
            if (!parse_line(line, cat, line_num)) {
                cout << "parse line: " << line << ", failed.." << endl;
                continue;
            }

            y(line_num) = cat;

            line_num += 1;
        }
        in.close();
    }
 }

bool debug = false;
using namespace boost::numeric::ublas;
vector<double> y(no_records);
matrix<double> x(no_records,no_features);
using namespace std;

template < class T>
void convert_from_string(T& value, const string& s)
{
stringstream ss(s);
ss >> value;
}

int get_cat(const string& data) {
int c;
convert_from_string(c, data);

return c;
} 


bool get_features(const string& data, int& index, double& value) {
int pos = data.find(":");
if (pos == -1) return false;
convert_from_string(index, data.substr(0, pos));
convert_from_string(value, data.substr(pos + 1));

return true;
}


bool parse_line(const string& line, int& cat, const int line_num) {
if (line.empty()) return false;
size_t start_pos = 0;
char space = ' ';

while (true) {
    size_t pos = line.find(space, start_pos);

    if ((int)pos != -1) {
        string data = line.substr(start_pos, pos - start_pos);
        if (!data.empty()) {
            if (start_pos == 0) {
                cat = get_cat(data);
            }
            else {
                int index = -1;
                double v = 0;
                get_features(data, index, v);
                if (debug)
                    cout << "index: " << index << "," << "value: " << v << endl;
                if (index != -1) {
                    index -= 1; // index from 0
                    x(line_num, index) = v;
                }
            }
        }
        start_pos = pos + 1;
    }
    else {
        string data = line.substr(start_pos, pos - start_pos);
        if (!data.empty()) {
            cout << "read data: " << data << endl;
            int index = -1;
            double v = 0;
            get_features(data, index, v);
            if (debug)
                cout << "index: " << index << "," << "value: " << v << endl;
            if (index != -1) {
                index -= 1; // index from 0
                x(line_num, index) = v;
            }
        }
        break;
    }
}

return true;
}
void load_数据(常量字符*文件名)
{
ifstream-in(文件名);
弦线;
int line_num=0;
if(in.is_open()){
while(in.good()){
getline(in,line);
如果(line.empty())继续;
int cat=0;
if(!parse_line(line,cat,line_num)){

我找到了罪魁祸首。
错误分配的原因是内存不足。问题是我使用了
密集矩阵
表示法(由boost库提供)因此,在boost矩阵表示法中将大小
20000x4000
的矩阵存储为稠密矩阵将需要大小
6.4GB
的RAM。现在,如果RAM中没有那么多空间,将弹出错误的分配。

它将占用大约相同的数量。是的。您得到的崩溃/异常可能与大小有关,但它是doesn不一定是,但是如果没有更多的信息,最好是一些代码,就不可能说任何事情。请尝试创建一个并向我们展示输入文件的小样本(如果是文本)。您所做的假设几乎肯定是无效的。36MB不算什么,文件大小也不是导致错误的直接原因。我只是在一个包含1000行和5K列(功能)的文件(硬盘上为42MB)上运行了我的程序。它运行得很好。但是,当我尝试使用包含70K行和20K列的数据文件运行相同的代码时,出现了错误的分配错误(同样在大小为10K X 1M的数据文件上)。这表明文件大小不是问题。但是,你能猜出为什么会出现这种错误吗?猜测在解决问题时不是一个有用的工具。再说一遍,你的程序在做什么?它在哪里分配内存?如何分配内存?等等。当我没有什么有用的东西可读时,不要责怪我的阅读理解。祝贺你现在实现了后者。