代码之家  ›  专栏  ›  技术社区  ›  Tryer

C++:GCC编译器警告大堆栈分配

  •  1
  • Tryer  · 技术社区  · 4 年前

    void largestackallocation() {
        double a[10000000];
    }
    
    int main() {
        return 0;
    }
    

    C6262 发出的命令建议将分配移动到堆而不是堆栈。编译处于发布模式,具有以下选项:

    /permissive- /ifcOutput "x64\Release\" /GS /GL /W3 /Gy /Zc:wchar_t /I"E:\local\boost_1_72_0" /Zi /Gm- /O2 /sdl /Fd"x64\Release\vc142.pdb" /Zc:inline /D "_MBCS" /errorReport:prompt /WX- /Zc:forScope /Gd /Oi /MD /FC /Fa"x64\Release\" /EHsc /nologo /Fo"x64\Release\" /FA /Fp"x64\Release\windows.pch" /diagnostics:column
    

    在gcc上,发布模式构建甚至可以将警告转换为错误:

    g++    -c -O2 -Werror -MMD -MP -MF "build/Release/GNU-Linux/_ext/511e4115/largestackallocation.o.d" -o build/Release/GNU-Linux/_ext/511e4115/largestackallocation.o ../src/largestackallocation.cpp
    mkdir -p dist/Release/GNU-Linux
    

    未发出任何警告/错误。使gcc/g++在潜在的大堆栈分配上发出警告的选项/设置是什么?

    1 回复  |  直到 4 年前
        1
  •  2
  •   ZiperZ The Unbeaten    4 年前

    -Wlarger than=“max bytes”可能是您要查找的内容。每当定义了大小超过“最大字节”的对象时,它都会发出警告。

    推荐文章