ingerbry
ingerbry

Reputation: 21

How to avoid Segmentation fault: 11; c++

I'm debugging a simple C++ script using gdb and see that I get an error when I try and initialize temp_grid. I try and compile it by running

g++ -Wall initial.cc -o initial

Is there a way to avoid this segmentation fault with something inside the script?

#include <iostream>
#include <array>
#include <valarray>
#include <stdlib.h>
#include <memory>

using namespace std;

int main()
{
  using std::array;
  array<array<float, 1024>, 1024> grid ={};

  // temp grid
  array<array<float, 1024>, 1024> temp_grid ={};

  return 0;
}

Upvotes: 2

Views: 1385

Answers (1)

TypeIA
TypeIA

Reputation: 17258

You are most likely overflowing the stack, which has relatively limited storage space for local variables. Try allocating them using dynamic storage (using new). For maximum robustness, use smart pointers (unique_ptr) to manage the pointers.

Upvotes: 5

Related Questions