You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

g++返回栈地址无警告问题咨询:为何部分场景无法触发警告?

g++返回栈地址不触发警告的问题咨询

问题现象

  • 旧版g++中,以下函数仅在使用-O3编译时才会触发返回栈地址的警告:
int *fun(int input)
{
    int i1=input;
    int i2=0;

    return i1>i2 ? &i1 : &i2;
}
  • 新版g++(启用-std=c++17和-Wall参数)中,以下函数无论采用何种编译方式都不会触发警告:
const char* getStr(const char* input)
{
    std::string s{input};

    return s.c_str();
}
  • 对比:clang编译器可通过-Wreturn-stack-address选项检测到上述第二个函数的问题。

已尝试的编译选项

已添加-Wall、-pedantic、-Wreturn-local-addr等编译选项,仍无法触发警告。

疑问

  1. 是否遗漏了某些编译配置?
  2. 该问题存在至少8年,为何至今未被解决?

内容的提问来源于stack exchange,提问作者SUMJL

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.15 14:15:54