如何处理MINGW中的小堆栈尺寸?

发布于 2025-01-25 05:19:15 字数 379 浏览 1 评论 0原文

当我在Linux虚拟机中对其进行测试时,我的代码

#include <stdio.h>
//#define arraySize 5
#define arraySize 500
void func(double B[arraySize][arraySize])
{
    B[0][0] = 5;
}

int main(void) {
    double Ar2D[arraySize][arraySize];
    func(Ar2D);
    printf("%f", Ar2D[0][0]);
}

可以正常工作,但是当我在mingw中运行它时,如果将阵列设置为500,它会崩溃(如果将数组设置为5,则在Mingw中正常工作)。解决此问题的最佳方法是什么?

I have the following code

#include <stdio.h>
//#define arraySize 5
#define arraySize 500
void func(double B[arraySize][arraySize])
{
    B[0][0] = 5;
}

int main(void) {
    double Ar2D[arraySize][arraySize];
    func(Ar2D);
    printf("%f", Ar2D[0][0]);
}

Code works fine when I test it in a linux virtual machine, but when I run it in minGW, it crashes if I set arraySize to 500 (works fine in minGW if I set arraySize to 5). What is the best way to fix this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

热情消退 2025-02-01 05:19:15

您真的应该避免将堆栈用于大事。

这也是安全风险,因为黑客滥用堆栈溢出WAN(例如,尝试尝试运行任意代码)。

更好的是使用堆,您可以通过替换为此来做到这一点

double Ar2D[arraySize][arraySize];

double** Ar2D = (double**)malloc(arraySize * arraySize * sizeof(double));

当然不要忘记free(ar2d);

You really should avoid using the stack for large things.

It's also a security risk as stack overflows wan be abused by hackers (e.g. to try to attempt running arbitrary code).

Better is to use the heap, you can do this by replacing

double Ar2D[arraySize][arraySize];

with

double** Ar2D = (double**)malloc(arraySize * arraySize * sizeof(double));

and of course don't forget to free(Ar2D);

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文