大数组,堆栈溢出和动态分配

时间:2017-01-14 00:50:37

标签: c++ arrays segmentation-fault stack-overflow

我想知道如何在不借助向量的情况下创建和启动这个大型数组。基本上,我一直在尝试创建一个大的一维数组,其每个元素是一个具有L = NGRID单元格的立方体的单元格。从参数文件中读取NGRID。

#include <stdio.h>
#include <stdlib.h>
#include <sstream>
#include <fstream>
#include <iostream>
#include <string>
#include <memory>


#define pow3(x) ((x)*(x)*(x))
#define at(ix,iy,iz) ((ix*NGRID*NGRID) + (iy*NGRID) + (iz))

std::string output_file="halo_snapshot_z0.txt"; // name of the output file
std::ofstream myoutput;

double SBOX;    // size of the box
int NGRID;      // number of grid cells

int main(int argc, char *argv[])
{
    std::ostringstream input_file;
    std::ostringstream para_file;

    input_file << argv[1];
    para_file << argv[2];
    std::string input_file_string = input_file.str();
    std::string para_file_string = para_file.str();

    FILE* pfile = std::fopen(para_file_string.c_str(),"r");
    if (!pfile) {
        std::cout << "Parameter file not found!" << std::endl;
    }
    std::fscanf (pfile, "%lf", &SBOX);
    std::cout << SBOX << std::endl;
    std::fscanf (pfile, "%d", &NGRID);
    std::cout << NGRID << std::endl;
    double SGRID = SBOX/NGRID;

    // Initiate the halo density array
    int* densh;
    densh = (int*) malloc(pow3(NGRID) * sizeof(int));
    std::uninitialized_fill_n(densh, pow3(NGRID), 0);

    // Read the halo catalogue
    std::ifstream ifs(input_file_string.c_str(),std::ifstream::in); // in C++, the string variable <filename> cannot be used as a parameter, instead one must use filename.c_str()
    std::string iline;

    while(std::getline(ifs, iline)) // Read one entire line from ifs
    {
        std::istringstream iss(iline); // Access the line as a stream
        // Take in only the first six columns
        long int id;
        long int host;
        int sub_no;
        double mass;
        int part_no;
        double x; // x coordinate in kpc
        double y; // y coordinate in kpc
        double z; // z coordinate in kpc
        double vx;
        double vy;
        double vz;
        double rvir;
        double rmax;
        float dummy1;
        float dummy2;
        float dummy3;
        float dummy4;
        float dummy5;
        float dummy6;
        float dummy7;
        float dummy8;
        float dummy9;
        float dummy10;
        float dummy11;
        float dummy12;
        float dummy13;
        float dummy14;
        float dummy15;
        float dummy16;
        float dummy17;
        float dummy18;
        float dummy19;
        float dummy20;
        float dummy21;
        float dummy22;
        float dummy23;
        int   dummy24;
        float dummy25;
        float dummy26;
        float dummy27;
        float dummy28;
        float dummy29;
        float dummy30;


        iss >> id >> host >> sub_no >> mass >> part_no >> x >> y >> z >> vx >> vy >> vz >> rvir >> rmax >> dummy1 >>  dummy2 >>  dummy3 >>  dummy4 >>  dummy5 >>  dummy6 >>  dummy7 >>  dummy8 >>  dummy9 >>  dummy10 >>  dummy11 >>  dummy12 >>  dummy13 >>  dummy14 >>  dummy15 >>  dummy16 >>  dummy17 >>  dummy18 >>  dummy19 >>  dummy20 >>  dummy21 >>  dummy22 >>  dummy23 >>  dummy24 >>  dummy25 >>  dummy26 >>  dummy27 >>  dummy28 >>  dummy29 >>  dummy30 ; // read the entire line

        // Select only host halos and assign them onto the grid

        if (host==0) {
            //NGP mass assignment
            float gx = x/SGRID-1.;
            float gy = y/SGRID-1.;
            float gz = z/SGRID-1.;

            int ix = (int)(gx+0.5);
            int iy = (int)(gy+0.5);
            int iz = (int)(gz+0.5);

            densh[at(ix,iy,iz)] += 1;
        }
    }

    myoutput.open (output_file.c_str(),std::ofstream::out);
    for (int i=0;i<pow3(NGRID);++i) {
        myoutput << densh[i] << '\n';
    }
    myoutput.close();
    myoutput.clear();

    return 0;
}

当我尝试使用NGRID = 250时,这会给我分段错误。 非常感谢你!

编辑:已修改为包含完整代码,抱歉,如果它看起来很乱,感谢您的时间!

0 个答案:

没有答案