堆栈溢出 C++

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6219878/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 19:42:22  来源:igfitidea点击:

Stack overflow C++

c++exceptionstack-overflowcallstack

提问by user570593

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

这是我的代码。当我在 initImg 函数中访问 dtr 数组时,它会给出堆栈溢出异常。可能是什么原因?

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

回答by Dr. Snoopy

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.

在堆栈中创建每个 4 * 1000 * 1000 ~ 4 MB 的数组。堆栈空间是有限的,通常小于 4 MB。不要那样做,使用 new 在堆中创建数组。

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

回答by Puppy

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.

您的堆栈可能不足以容纳一百万个整数和一百万个浮点数 (8MB)。因此,一旦您尝试访问超出堆栈大小的内容,您的操作系统就会向您抛出错误。超过特定大小的对象或数组需要在堆上分配 - 最好使用自我管理的自我边界检查类,例如std::vector- 具体大小取决于您的实现。

回答by mah

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

除了堆栈溢出之外,您还有另一个问题——您对 W 和 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.

您的 i 循环应该从 0 计数到 H-1,而不是 W-1(并且 j 循环也应该交换)。否则你的代码只有在 W==H 时才能正常工作。如果 WH 你会溢出你的缓冲区。

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

您的代码示例中的其他地方也存在同样的问题。

回答by Mark B

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

您正在堆栈上创建巨大的数组。只需使用std::vector

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

回答by Adrian

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.

您的堆栈已满。您可以在堆中分配内存或增加堆栈内存。据我所知,最大大小约为 8MB,但这不是一个好主意。最好的解决方案是使用堆分配或 std 中可用的一些容器(向量)。

回答by Bo Persson

You will eventually get to

你最终会得到

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

Which is much more than you have allocated.

这比您分配的要多得多。

回答by Adrian Rodriguez

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

您的编译器将定义堆栈大小。解决此问题的一种方法是使用 std::vector array_one(W*H) 动态分配数组。

回答by Anonymous

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

您也可以使用 new 而不是使用 malloc 从堆分配内存...