user570593
user570593

Reputation: 3520

Stack overflow C++

This is my code. When I access dtr array in initImg function it gives a stack overflow exception. What might be the reason?

#define W 1000
#define H 1000
#define MAX 100000 
void initImg(int img[], float dtr[])
{
    for(int i=0;i<W;i++)
        for(int j=0;j<H;j++)
            img[i*W+j]=255;

    for(int j=0;j<H;j++)
    {
        img[j] = 0;
        img[W*(W-1)+j] = 0;
    }
    for(int i=0;i<W;i++)
    {
        img[i*W] = 0;
        img[i*W+H-1] = 0;
    }
    for(int i=0;i<W;i++)
        for(int j=0;j<H;j++)
        { 
            if(img[i*W+j]==0)
                dtr[i*W+j] = 0;    // <------here
            else
                dtr[i*W+j] = MAX;  // <------here
        }
}
int main()
{
    int image[W*H];
    float dtr[W*H];
    initImg(image,dtr);
    return 0;
}

Upvotes: 10

Views: 56144

Answers (8)

Adrian
Adrian

Reputation: 1025

Your stack is full. You can allocate memory in heap or increase the stack memory. From what I know the maximum size is about 8MB, but this is not a very good idea. The best solution is to use heap allocation or some containers (vector) available in std.

Upvotes: 1

Anonymous
Anonymous

Reputation: 1

You are trying to allocate memory from stack. the maximum memory which can be allocated using stack is complier dependent. So try something like this to avoid this kind of exception.

#include <stdlib.h>
#define W 1000
#define H 1000 
#define MAX 100000 
void initImg(int img[], float dtr[]) 
{ 
for(int i=0;i<W;i++) 
for(int j=0;j<H;j++) 
img[i*W+j]=255; 

for(int j=0;j<H;j++) 
{ 
img[j] = 0; 
img[W*(W-1)+j] = 0; 
} 
for(int i=0;i<W;i++) 
{ 
img[i*W] = 0; 
img[i*W+H-1] = 0; 
} 
for(int i=0;i<W;i++) 
for(int j=0;j<H;j++) 
{ 
if(img[i*W+j]==0) 
dtr[i*W+j] = 0; // <------here 
else 
dtr[i*W+j] = MAX; // <------here 
} 
} 
int main() 
{ 
int *image = (int*)malloc(4*W*H);   //Malloc the memory....(Allocated from Heap..)
float *dtr = (float*)malloc(4*W*H);

if(image && dtr) //If none of the ptr is NULL. Means memory is allocated...
{
initImg(image,dtr); 
}
return 0; 
}

You can use new as well instead of using malloc to allocate memory from heap...

Upvotes: -4

mah
mah

Reputation: 39817

In addition to the stack overrun, you have another problem -- one which is masked by your definitions of W and H.

for(int i=0;i<W;i++)
    for(int j=0;j<H;j++)
    { 
        if(img[i*W+j]==0)
            dtr[i*W+j] = 0;    // <------here
        else
            dtr[i*W+j] = MAX;  // <------here
    }

Your i loop should count from 0 to H-1, rather than W-1 (and the j loop should swap as well). Otherwise your code will only work correctly if W==H. If WH you will overrun your buffers.

This same problem exists elsewhere in your code sample as well.

Upvotes: 4

Mark B
Mark B

Reputation: 96281

You're creating giant arrays on the stack. Just use std::vector instead:

std::vector<int> image(W*H);
std::vector<float> dtr(W*H);

Upvotes: 2

Adrian Rodriguez
Adrian Rodriguez

Reputation: 3242

Your compiler will define the stack size. A way to get around this is to dynamically allocate your arrays using std::vector array_one(W*H).

Upvotes: 0

Bo Persson
Bo Persson

Reputation: 92321

You will eventually get to

dtr[W*W+j] = 0;   <------here

Which is much more than you have allocated.

Upvotes: 0

Puppy
Puppy

Reputation: 146968

Your stack probably isn't big enough to hold a million ints and a million floats (8MB). So as soon as you try to access beyond your stack size, your operating system throws you an error. Objects or arrays above a certain size need to be allocated on the heap - preferably using a self-managing self-bounds-checking class such as std::vector - the specific size depends on your implementation.

Upvotes: 11

Dr. Snoopy
Dr. Snoopy

Reputation: 56377

This:

int image[W*H];
float dtr[W*H];

Creates each a 4 * 1000 * 1000 ~ 4 MB array into the stack. The stack space is limited, and usually it's less than 4 MB. Don't do that, create the arrays in the heap using new.

int *image = new int[W*H];
float *dtr = new float[W*H];

Upvotes: 20

Related Questions