最近做一个C++开源项目发现一个奇怪问题,通过clang编译链接执行程序每到有一个就崩溃了,gcc下则没有此问题。

后来通过调试,发现原因是bool返回的方法是没有return语句!问题是为啥还能通过编译呢?

列子如下:

#include <iostream>

class Test {
public:
bool yes();
}; bool Test::yes() {
std::cout << "yes" << std::endl;
// return false;
}; int main() {
Test *t = new Test;
bool r = t->yes();
std::cout << "yes->" << r << std::endl;
return 0;
}

用g++编译得到警告但是通过了,并且执行得到正确预期(但是值为啥是64?没有找到原因!)。

1 gaojie@root-host:~$ g++ bool.cpp
2 bool.cpp: In member function ‘bool Test::yes()’:
3 bool.cpp:11:1: warning: no return statement in function returning non-void [-Wreturn-type]
4 11 | };
5 | ^
6 gaojie@root-host:~$ ./a.out
7 yes
8 yes->64

用clang++编译同样类似警告也通过了,但执行出现异常指令。

gaojie@root-host:~$ clang++ bool.cpp
bool.cpp:12:1: warning: non-void function does not return a value [-Wreturn-type]
};
^
1 warning generated.
gaojie@root-host:~$ ./a.out
yes
非法指令 (核心已转储)

本着好奇的心理,就想知其原因为啥会有不一样的结果呢?就想通过汇编语法查询差异。

通过 https://godbolt.org/得到如下:

g++汇编指令如下:

clang++汇编指令如下:

通过yes方法发现差异了,

gcc汇编有return指令可以正常返回。

而clang就ud2指令结束了,查阅相关资料得到,UD2是一种让CPU产生invalid opcode exception的软件指令.  内核发现CPU出现这个异常, 会立即停止运行.

问题原因找到了就是gcc和llvm对编译申明返回值方法而没有返回语句的处理结果是不一样的,g++保证通过而clang则认为无法处理(给了异常指令)退出程序。

[C++] - GCC和LLVM对方法 warning: non-void function does not return a value [-Wreturn-type] 的处理差异的更多相关文章

  1. 转:GCC,LLVM,Clang编译器对比

    GCC,LLVM,Clang编译器对比   转自: http://www.cnblogs.com/qoakzmxncb/archive/2013/04/18/3029105.html 在XCode中, ...

  2. GCC,LLVM,Clang编译器对比

    http://www.cnblogs.com/qoakzmxncb/archive/2013/04/18/3029105.html   在XCode中,我们经常会看到这些编译选项(如下图),有些人可能 ...

  3. linux系统下,警告:warning: implicit declaration of function ‘gets’ [-Wimplicit-function-declaration] 和 warning: the `gets' function is dangerous and should not be used. 的由来和解决方法。

    字符数组 的英文名字是 char [] gets()函数的基本用法为:char *gets(char *s); 该函数的参数是一个字符数组,该函数的返回值也是一个字符数组. linux下的代码如下: ...

  4. warning: the `gets' function is dangerous and should not be used.(转)

    今天在LINUX下编译C程序时,出现了:warning: the `gets' function is dangerous and should not be used. 这个warning. 百度之 ...

  5. warning: Unexpected unnamed function (func-names)

    warning: Unexpected unnamed function (func-names) 看到这个提示基本是就是说你的函数不能是匿名函数,最好可以起一个名字,然后你增加一个函数名称就好了 R ...

  6. Apple LLVM 6.0 Warning: profile data may be out of date

    I have no clue what this meant, so I googled the problem. I only ended up with some search results s ...

  7. 编译器:gcc, clang, llvm

    clang Clang是LLVM的前端,可以用来编译C,C++,ObjectiveC等语言.传统的编译器通常分为三个部分,前端(frontEnd),优化器(Optimizer)和后端(backEnd) ...

  8. 38.利用接口做参数,写个计算器,能完成+-*/运算 (1)定义一个接口Compute含有一个方法int computer(int n,int m); (2)设计四个类分别实现此接口,完成+-*/运算 (3)设计一个类UseCompute,含有方法: public void useCom(Compute com, int one, int two) 此方法要求能够:1.用传递过来的对象调用comp

    //接口Compute package jieKou; public interface Compute { int Computer(int n,int m); } //加 package jieK ...

  9. 【Flask】报错解决方法:AssertionError: View function mapping is overwriting an existing endpoint function: main.user

    运行Flask时出现了一个错误, AssertionError: View function mapping is overwriting an existing endpoint function: ...

随机推荐

  1. python压缩pdf(指定缩放比例)

    python压缩pdf(指定缩放比例) 原理 pdf文件处理使用https://pymupdf.readthedocs.io/en/latest/index.html库可以轻松实现,该库的官方说明文档 ...

  2. KingbaseES 数据库Windows环境下注册数据库服务

    关键字: KingbaseES.Java.Register.服务注册 一.安装前准备 1.1 软件环境要求 金仓数据库管理系统KingbaseES V8.0支持微软Windows 7.Windows ...

  3. Linux_etc-passwd文件总结

    文件内容 ## # User Database # # Note that this file is consulted directly only when the system is runnin ...

  4. git hooks在业务中的使用

    起因 最近公司项目发生了一起线上事故,最后排查下来是配置文件的问题.项目里application.yml文件内会用@build.time@记录打包时的时间,但是这个写法是build-helper-ma ...

  5. 使用Pipeline抽象业务生命周期流程

    上篇关于流程引擎的文章还是快两年以前的<微服务业务生命周期流程管控引擎>,这中间各种低代码平台层出不穷,虽然有些仅仅是OA+表单的再度包装,但有些的确是在逻辑和操作单元层面进行了真正的高度 ...

  6. 跟羽夏学 Ghidra ——数据

    写在前面   此系列是本人一个字一个字码出来的,包括示例和实验截图.本人非计算机专业,可能对本教程涉及的事物没有了解的足够深入,如有错误,欢迎批评指正. 如有好的建议,欢迎反馈.码字不易,如果本篇文章 ...

  7. Lua CallbackHell优化

    概述 在异步操作中,常常要使用回调.但是,回调的嵌套常常会导致逻辑混乱,一步错步步错,难以维护.在Lua中,可以使用协程进行优化. 问题分析 模拟一个回合制游戏攻击过程 local function ...

  8. linux下安装Elasticsearch(单机版和集群版)

    一.linux下安装Elasticsearch(单机) 1.软件下载 下载地址:https://www.elastic.co/cn/downloads/past-releases/elasticsea ...

  9. uniapp路由守卫

    项目地址:https://hhyang.cn/v2/start/quickstart.html ​ 按照他的方法安装,创建相应的js即可,有点基础的自己捣鼓一下就可以了.我的应用场景是:没有登录痕迹- ...

  10. ProxySQL的双层用户认证机制

    转载自:https://www.likecs.com/show-203802325.html 如果使用了ProxySQL来做中间路由,那么与我们平时登录数据库有一些区别:平时我们直接使用数据库的用户密 ...