Stack overflow C++

2019-01-20 10:59发布

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

8条回答
Rolldiameter
2楼-- · 2019-01-20 10:59

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.

查看更多
姐就是有狂的资本
3楼-- · 2019-01-20 11:02

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.

查看更多
一纸荒年 Trace。
4楼-- · 2019-01-20 11:02

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];
查看更多
Deceive 欺骗
5楼-- · 2019-01-20 11:03

You will eventually get to

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

Which is much more than you have allocated.

查看更多
相关推荐>>
6楼-- · 2019-01-20 11:04

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...

查看更多
干净又极端
7楼-- · 2019-01-20 11:08

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);
查看更多
登录 后发表回答