-1

I have a class that where I want to store three datapoints for each pixel in an image. I thought std::tuple would be a nice way to do this so:

class CameraManager {
private:
    static const int width_  = 700;
    static const int height_ = 574;
     //this causes a segfault...
    std::tuple<double,double,bool> mapping_[width_][height_];

public:
    CameraManager();

}

The segfault happens directly at main(int argc, char ** argv) because I have a camera manager object declared in this function.

What is going on here?

user1443778
  • 581
  • 1
  • 5
  • 20

2 Answers2

2

You're putting something like 6MB on your call stack with that. It's really at the sort of size where you want to avoid automatic storage duration.

Try allocating it dynamically.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
  • Makes sense. Just curious, what would happen if I defined the array as a global variable in e.g. the file where main.cpp is defined. Where would the memory be allocated, text part of the program? – user1443778 Oct 16 '14 at 09:12
1

Let's make some calculations: an std::tuple<double,double,bool> costs roughly 24 bytes on some machines and therefore a matrix 700x574 will cost about 9'643'200 bytes, which is ~10MB.

Most implementations allocate 1MB, maybe 2MB of stack size. If your implementation allows you to specify a bigger stack size, then set that to something greater than ~12MB.

Otherwise allocate mapping_ dynamically:

using my_tuple = std::tuple<double,double,bool>;
std::vector<my_tuple> mapping_;

and then:

CameraManager() : mapping_(width_ * height_) { ... }

and maybe create a simple matrix view over that vector.

Shoe
  • 74,840
  • 36
  • 166
  • 272