堆叠溢出的巨大文件,其中有许多变量在不同的范围中

发布于 2025-01-29 17:20:04 字数 930 浏览 2 评论 0原文

我有一个自动化文件创建结构并对其进行一些计算。 每个结构都有其专用范围。

typedef struct
{
    uint16_t a;
    uint16_t b;
} Addition_t;


uint8_t StructsOverflow(void)
{
    { // use new scope to declare same variable multiple times
        Addition_t x = {.a = 5, .b=6};
        if (x.a == x.b)
            return 1;
    }
    {
        Addition_t x = {.a = 3, .b=6};
        if (x.a == x.b) 
            return 1;
    }
    {
        Addition_t x = {.a = 3, .b=5};
        if (x.a == x.b) 
            return 1;
    }
    // and so on
    // here other structs are created in the same fashion as above
    return 0;
}

对于大量的行(约100,000个结构),运行.exe在stackoverflow中停止:异常在energyPredictionmain.exe中以0x00007fly thf Fifflion:0x0000007f2c8b6c8抛出:0xc00000fd:stack Overflow(参数:0x00000000000000000001,0x0000000000000000000000000015603000)。

IM使用MSVC 2019编译器和CPPVSDBG进行调试。

为什么有堆栈流?在我的理解中,变量在范围之后被破坏,因此仅应使用一个结构的内存。

I have a autogenerated file creating structs and doing some calculations with them.
Each struct has its dedicated scope.

typedef struct
{
    uint16_t a;
    uint16_t b;
} Addition_t;


uint8_t StructsOverflow(void)
{
    { // use new scope to declare same variable multiple times
        Addition_t x = {.a = 5, .b=6};
        if (x.a == x.b)
            return 1;
    }
    {
        Addition_t x = {.a = 3, .b=6};
        if (x.a == x.b) 
            return 1;
    }
    {
        Addition_t x = {.a = 3, .b=5};
        if (x.a == x.b) 
            return 1;
    }
    // and so on
    // here other structs are created in the same fashion as above
    return 0;
}

For a huge number of Lines (about 100,000 structs), running the .exe stops with a StackOverflow: Exception thrown at 0x00007FF7F2C8B6C8 in EnergyPredictionMain.exe: 0xC00000FD: Stack overflow (parameters: 0x0000000000000001, 0x0000001815603000)..

Im using the MSVC 2019 compiler and cppvsdbg for debugging.

Why is there an stackoverflow? In my understanding the variables are destroyed after the scope, so only the memory of one struct should be used.

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

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

发布评论

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

评论(1

揽月 2025-02-05 17:20:04

为什么?因为,在调试构建(IIRC)中,MSVC以这种方式脱离范围时不会对其进行分配。在发布版本中,它可能会起作用。

但是,这里真正破裂的是,IMO就是自动基因的该文件。将其更改为生成100,000个单独的子函数,每个初始化(然后是处理)一个结构是否实用?然后从(也是自动生成的)“主”功能中调用它们中的每个功能。

如果您可以做到这一点,它应该提供一个健壮且适合未来的修复程序。

Why? Because, in a Debug build (IIRC), MSVC doesn't deallocate local variables when they go out of scope in this way. In a Release build, it will probably work.

But what's really broken here, IMO, is whatever it is that autogenerates that file. Would it be practical to change it to generate 100,000 separate sub-functions, each initialising (and then processing) one struct? Then invoke each of them in turn from an (also auto-generated) 'master' function.

If you can do that, it should provide a robust and future-proof fix.

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