Stack overflow C++

前端 未结 8 2010
时光说笑
时光说笑 2020-12-02 00:06

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
#de         


        
相关标签:
8条回答
  • 2020-12-02 00:50

    You will eventually get to

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

    Which is much more than you have allocated.

    0 讨论(0)
  • 2020-12-02 00:52

    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.

    0 讨论(0)
提交回复
热议问题