简体   繁体   中英

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;
}

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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