0

hey all i would just like to as why i keep getting a stack overflow error whenever i try to initialize a 512x512 array? Can anyone help? below is part of my code

CImg<float> image("lena8bit.jpg"); 
CImgDisplay main_disp(image,"Main image");

    int ImgArray [512][512];

Basically all i want to do is get the pixel values from the image and store it into this array. The image is 512x512 hence the array size.

Hope to hear you answers, thanks!

  • 3
    what are the problems you have with it? – Ivaylo Strandjev Jun 02 '12 at 09:25
  • Either you are working on a machine with very limited memory (unlikely), or the problem is somewhere else in your code. `int ImgArray[512][512];` compiles fine on my system. What exactly is the error you are getting? – Moritz Jun 02 '12 at 09:25
  • It's possible there is already a fair bit on the stack and it's asking for more than 1MB of stack space which quite a lot of implementations will choke on. – Flexo Jun 02 '12 at 09:27
  • Ah, I didn't know that >1MB of stack space was problematic. Thanks for pointing that out. – Moritz Jun 02 '12 at 09:31

5 Answers5

5

Your array is too big to be allocated on the stack.

You will have to allocate it on the heap, with new[] (and use delete[] for deallocation).

So, you can create the array like that:

// Create the array
int ** myArray = new int*[512];
for(int i=0; i<512; i++)
    myArray[i] = new int [512];

myArray[12][64] = 52; // Work with the array as you like

// Destroy the array
for(int i = 0 ; i < 512 ; i++ )
    delete [] myArray[i];
delete [] myArray;
Mesop
  • 5,187
  • 4
  • 25
  • 42
1

The other answers have shown ways you can allocate memory for images in 2-D arrays not on the stack, but usually when working with images it's better to work with a 1-D array and index into that directly, e.g.:

const int width=512, height=512;
std::vector<int> pixels(height*width);

You can then find a pixel at a specific coordinate:

// Find an x and y position:
int x=12, y=12;
int &px = pixels.at((y*width) + x);

Or find the coordinates of a specific pixel:

// Find the x and y values of pixel 100;
int idx = 100;
x = idx % width;
y = idx / width;

These are all simple integer operations and by doing it like this you will only have one, contiguous block of memory to worry about per image. You can let something like std::vector manage that memory for you in a clean safe RAII way.

Flexo
  • 87,323
  • 22
  • 191
  • 272
1

I see two solutions that haven't been mentioned yet. You could use memory with static storage duration:

static int ImgArray [512][512];

Note that the array will live for the entirety of your program if it is declared as static. This can be a problem if you plan to call the function multiple times from different threads or if the function is recursive.

Or you could allocate the array from the heap and manage the lifetime by a unique pointer:

std::unique_ptr<std::array<std::array<int, 512>, 512>> p
           (new std::array<std::array<int, 512>, 512>);

The syntax looks a lot less convoluted if you write a little helper function:

auto p = make_unique<std::array<std::array<int, 512>, 512>>();
Community
  • 1
  • 1
fredoverflow
  • 256,549
  • 94
  • 388
  • 662
0

normally, in 32bit OS, the stack of each thread has default size 4K.

So you array is large than it. you can do

  1. define the array as global or static variable which are not allocated in stack.

  2. use the new/malloc to allocate on heap

RolandXu
  • 3,566
  • 2
  • 17
  • 23
0

Answer depends on what platform you are using. For example Microsoft Visual Studio uses 1MB stack size by default. You can change the default stack size of the application by using the /STACK linker option.

On Linux it's a bit different, probably this may help you.

But using dynamic memory allocation is more appropriate in your case I think.

ImgArray** arr = new ImgArray* [512];
for(int i =0; i< 512; i++) arr[i] = new ImgArray[512];
Community
  • 1
  • 1
VinS
  • 194
  • 8