Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to work around a very large 2d array in C++

I need to create a 2D int array of size 800x800. But doing so creates a stack overflow (ha ha).

I'm new to C++, so should I do something like a vector of vectors? And just encapsulate the 2d array into a class?

Specifically, this array is my zbuffer in a graphics program. I need to store a z value for every pixel on the screen (hence the large size of 800x800).

Thanks!

like image 680
Bryan Denny Avatar asked Sep 14 '08 22:09

Bryan Denny


1 Answers

You need about 2.5 megs, so just using the heap should be fine. You don't need a vector unless you need to resize it. See C++ FAQ Lite for an example of using a "2D" heap array.

int *array = new int[800*800];

(Don't forget to delete[] it when you're done.)

like image 70
Adam Mitz Avatar answered Sep 23 '22 16:09

Adam Mitz