[英]what is causing Unhandled exception at memory : Stack overflow
I'm new to C++ and I'm implementing the Canny edge detector algorithm by myself.我是 C++ 的新手,我正在自己实现 Canny 边缘检测器算法。 for that, I've declared some 2D arrays.
为此,我声明了一些 2D arrays。 it gives me the error " Unhandled exception at 0x000A1809 in ConsoleApplication3.exe: 0xC00000FD: Stack overflow (parameters: 0x00000000, 0x00E92000)"
它给了我错误“ConsoleApplication3.exe 中 0x000A1809 处的未处理异常:0xC00000FD:堆栈溢出(参数:0x00000000、0x00E92000)”
I minimized the number of elements of my arrays, and it worked.我最小化了我的 arrays 的元素数量,并且它起作用了。 but when I need arrays with more elements
但是当我需要更多元素的 arrays
//2.Gradian Calculation: a.Calculating Gx and Gy b.Calculation sqrt(Gx^2 + Gy^2) c.Calculating the gradiant orientation
int Fx[3][3] = { {-1,0,1},{-2,0,2},{-1,0,1} };
int Fy[3][3] = { {1,2,1},{0,0,0},{-1,-2,-1} };
int image[100][100] = { 50 };
int gradian_x[100][100] = { 0 };
int gradian_y[100][100] = { 0 };
double edgeStrength[100][100] = { 0 };
//Calculating Gx
for (int i = 1; i < 99; i++)
{
for (int j = 1; j < 99; j++)
{
gradian_x[j][i] = image[j - 1][i - 1] * Fx[0][0] + image[j][i - 1] * Fx[1][0] + image[j + 1][i - 1] * Fx[2][0] + image[j + 1][i + 1] * Fx[2][2] + image[j][i + 1] * Fx[1][2] + image[j - 1][i + 1] * Fx[0][2];
}
}
Stack space is limited.堆栈空间有限。 Provided that
int
is 4 bytes on your platform and double
is 8 bytes, you ask for 195 kiB memory, which may or may not be available in stack (that's ignoring any other necessary things from stack like function calls etc).假设
int
在您的平台上是 4 个字节,而double
是 8 个字节,您要求 195 kiB memory,它可能在堆栈中可用也可能不可用(忽略堆栈中任何其他必要的东西,如 function 调用等)。
For storing larger sets of data, you should use dynamic memory allocation with std::vector
(it uses heap memory instead of stack memory, which is much larger than stack):为了存储更大的数据集,您应该使用带有
std::vector
的动态 memory 分配(它使用堆 memory 而不是堆栈 memory,它比堆栈大得多):
const int initialValue = 50;
std::vector<std::vector<int>> image (100, std::vector<int>(100, initial_value);
Note that int image[100][100] = { 50 };
注意
int image[100][100] = { 50 };
will initialize only image[0][0]
to 50 and the rest of the e lements to 0. It is fixed in above example (all elements are initialized with 50 there), but if you want to retain that behaviour, you should make initial_value
to be equal to 0 and change image[0][0]
directly.将仅将
image[0][0]
初始化为 50,并将元素的 rest 初始化为 0。它在上面的示例中是固定的(所有元素都在那里初始化为 50),但如果你想保留这种行为,你应该做initial_value
等于 0 并直接更改image[0][0]
。
For increased performance, you should preferable use a specialized library like Eigen
.为了提高性能,您最好使用像
Eigen
这样的专用库。 If you don't want external resources, you may want to use std::vector<std::array<int, 100>>
instead, which will allocate memory in single block (better for processor cache):如果您不想要外部资源,您可能想要使用
std::vector<std::array<int, 100>>
代替,它将在单个块中分配 memory (更适合处理器缓存):
std::array<int, 100> initialArray;
std::fill(initialArray.begin(), initialArray.end(), 50);
std::vector<std::array<int, 100>> image (100, initialArray);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.