简体   繁体   English

二进制分段错误 11

[英]segmentation fault 11 in binary

I'm trying to write to a binary file, such that I have time, id1, id2, and two garbage bytes (0) per line.我正在尝试写入一个二进制文件,以便每行有时间、id1、id2 和两个垃圾字节 (0)。 When I do this without the garbage bytes, it works, but once I add the garbage bytes, I get "segmentation fault 11" as an error.当我在没有垃圾字节的情况下执行此操作时,它可以工作,但是一旦我添加了垃圾字节,我就会收到“分段错误 11”作为错误。 I'm sure the CastorArray2 is written correctly (checked before this block of code).我确定CastorArray2已正确写入(在此代码块之前检查过)。

Here is my code:这是我的代码:

    struct Packet
    {
    //int64_t Time;                                                                                                                                                                                                                              
    uint32_t Time;
    uint32_t Crystal_ID1;``
    uint32_t Crystal_ID2;
    int Garbage1;
    int Garbage2;
    };
    printf("working before opening file");
    ofstream myfile;
    myfile.open("/Users/Desktop/Archive/WorkNEW.dat", ios::binary | 
    ios::out);
    if (myfile.is_open()) {
        printf("file open");
     // cout<< "cannot open file!" << endl;                                                                                                                                                                                                 
     // return 1;                                                                                                                                                                                                                           
    }
    Packet Event[495842];
    for (int n=0; n<495842; n++)
    {
        Event[n].Time=1.0;
        //       Event[n].Time=CastorArray2[n][3];                                                                                                                                                                                          
        Event[n].Crystal_ID1=CastorArray2[n][1];
        Event[n].Crystal_ID2=CastorArray2[n][2];
        Event[n].Garbage1=0;
        Event[n].Garbage2=0;
    }
    if (myfile.is_open()) {
        printf("Works");

     //   cout<<"file opened successfully"<<endl;                                                                                                                                                                                           
     //   return 1;                                                                                                                                                                                                                         
        for (int m=0;m<495842;m++)
        {
                                                                                                                                                   
          myfile.write((char*) &Event[m], sizeof(Packet));
        }
        myfile.close();
    }
    else {
        printf("notworking");
     //cout<< "unable to open file"<<endl;                                                                                                                                                                                                  
        return 1;
    }
    
   //}                                                                                                                                                                                                                                      
    
    return 0;

If I understand your code correctly you are allocating way too much on your stack frame.如果我正确理解您的代码,那么您在堆栈框架上分配的方式太多了。

Packet Event[495842];

495842 instances of Packet sums to about 20 MB of stack memory. 495842 个 Packet 实例总计约 20 MB 的堆栈内存。

A simple solution would be一个简单的解决方案是

std::vector<Packet> event(495842);

For the "new to c++ comment", std::vector allows you to use [index] syntax in the same fashion you use it on arrays.对于“c++ 新手注释”,std::vector 允许您以在数组上使用它的相同方式使用 [index] 语法。

@user451301 makes a good point. @ user451301 提出了一个很好的观点。 The stack is created when the program is started.堆栈是在程序启动时创建的。 It doesn't change afterwards.之后不会改变。 It is meant to hold the state of function calls including variables and arrays.它用于保存函数调用的状态,包括变量和数组。 In this case a function call exceeds the available stack space and forcibly terminates the program.在这种情况下,函数调用超出了可用的堆栈空间并强行终止程序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM