堆栈溢出C ++

编程入门 行业动态 更新时间:2024-10-23 17:27:21
本文介绍了堆栈溢出C ++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

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

推荐答案

This:

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

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

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

更多推荐

堆栈溢出C ++

本文发布于:2023-11-09 09:53:32,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1572070.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:堆栈

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!