-2

I'm trying to write to a binary file, such that I have time, id1, id2, and two garbage bytes (0) per line. 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. I'm sure the CastorArray2 is written correctly (checked before this block of code).

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;
JaMiT
  • 14,422
  • 4
  • 15
  • 31
  • 2
    Please assume that I am blind and that my screenreader cannot handle your link to a picture of text. https://meta.stackoverflow.com/questions/285551/why-not-upload-images-of-code-on-so-when-asking-a-question/285557#285557 – Yunnosch Jul 13 '21 at 20:14
  • In addition to accessibility, you should also include your code as text because it lets people copy-paste from it and try to run it themselves. – Dave S Jul 13 '21 at 20:16
  • 1
    thanks for the suggestion, never did this before so i didn't know, hopefully its better now? – mnquestion Jul 13 '21 at 20:18
  • 3
    That's an awful lot of memory for a stack allocation. About 64 Meg if my napkin is not too wet. – Captain Giraffe Jul 13 '21 at 20:20
  • @CaptainGiraffe do u mean the 495842 lines in the Event packet? It's supposed to have that many based on the acquired data, and for each line it should have a timestamp, id1, id2, and the two garbage bytes. Let me know if I misunderstood you, im pretty new to cpp so im not sure what u mean – mnquestion Jul 13 '21 at 20:25
  • 1
    @mnquestion Trying to code in C++ without having a good understanding of how it handles memory etc. will usually result in a great deal of frustration and unhappiness. Please start with the basics either using a good book or a good tutorial. – dandan78 Jul 13 '21 at 20:36
  • 2
    Before copying your code into your question, you should simplify it to a [mre]. Do you get the segmentation fault if you remove your `else` clause? (I'm working from the end of your code.) If you remove the `if` clause that used to have an `else`? Just how much can you remove while retaining the error? *(If the current answer is accurate, you should be able to get your code down to your `Packet` definition plus `int main() { Packet Event[495842]; }`. This would have allowed you to focus on the actual problem, ignoring red herrings like setting values and writing to a file.)* – JaMiT Jul 13 '21 at 21:33

1 Answers1

3

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.

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.

@user451301 makes a good point. 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.

Captain Giraffe
  • 14,407
  • 6
  • 39
  • 67
  • 2
    Addendum: Stack memory is often a tightly controlled resource capable of 1 to 10 MB of storage on a typical desktop PC. Exceeding this amount results in a Stack Overflow, the dreaded condition that named this site. – user4581301 Jul 13 '21 at 20:27
  • Another important bit about `vector` is while it should be allocated on the stack, it manages a pool of memory that is not allocated on the stack, instead taking its memory from a larger pool of memory called the [Free Store](https://stackoverflow.com/questions/1350819/c-free-store-vs-heap) – user4581301 Jul 13 '21 at 20:30