Weird error when reading a large .txt file in c++

This question already has an answer here:

  • Trouble getting memory allocated for char* in c++ 1 answer

  • float* hu_geometry = new float(dim);
    int* hu_temp = new int(dim);
    

    those are 1-char arrays containing the value dim . At some point you're hitting a MMU boundary and crashes randomly.

    You want to write:

    float* hu_geometry = new float[dim];
    int* hu_temp = new int[dim];
    

    or maybe better with vectors, pre-allocated with dim elements

    #include <vector>
    std::vector<float> hu_geometry(dim);
    std::vector<int> hu_temp(dim);
    

    or not allocated at start:

    std::vector<int> hu_temp;
    

    and in your code:

    hu_temp.push_back(stoi(line));
    

    ( hu_temp.size() gives the size and a lot of very nice features better described here)

    链接地址: http://www.djcxy.com/p/43884.html

    上一篇: 我有segfaults!

    下一篇: 在c ++中读取大型.txt文件时出现奇怪的错误