我是靠谱客的博主 优秀棒棒糖,最近开发中收集的这篇文章主要介绍clang++的诡异之处,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

最近一次作业,写一个高精度+ - × / 运算

我设计了一个bigint类用来实现高精度(代码会在下一篇贴出来)

当然写的时候有点小错误,程序运行一直失败,在调试的时候居然发现一件诡异的事情

我个人认为是clang++有问题,但通常说clang++编译更严格,也可能是我写法的问题

望各位指正


amrzs@ubuntu:bigint$ g++ -g main.cpp bigint.cpp -o gtst
amrzs@ubuntu:bigint$ clang++ -g main.cpp bigint.cpp -o ctst

amrzs@ubuntu:bigint$ gdb gtst
GNU gdb (GDB) 7.6.1-ubuntu
Copyright (C) 2013 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-linux-gnu".
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>...
Reading symbols from /home/amrzs/workspace/py3/security/bigint/gtst...done.
(gdb) b 48
Breakpoint 1 at 0x400cbd: file main.cpp, line 48.
(gdb) r
Starting program: /home/amrzs/workspace/py3/security/bigint/gtst
2000000000
100
10
2000000000
3
100
Breakpoint 1, main () at main.cpp:49
49
Bigint result = a / b;
(gdb) s
Bigint::operator/ (this=0x7fffffffc720, x=...) at bigint.cpp:104
104
Bigint result;
(gdb) n
105
result.clear(); //make result being zero
(gdb) n
107
if(size < x.size)
(gdb) p result
$1 = {static MAX_SIZE = 500, size = 1, arr = {0 <repeats 500 times>}}
(gdb) q

amrzs@ubuntu:bigint$ gdb ctst
GNU gdb (GDB) 7.6.1-ubuntu
Copyright (C) 2013 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-linux-gnu".
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>...
Reading symbols from /home/amrzs/workspace/py3/security/bigint/ctst...done.
(gdb) b 48
Breakpoint 1 at 0x400e4d: file main.cpp, line 48.
(gdb) r
Starting program: /home/amrzs/workspace/py3/security/bigint/ctst
2000000000
100
10
2000000000
3
100
Breakpoint 1, main () at main.cpp:49
49
Bigint result = a / b;
(gdb) s
Bigint::operator/ (this=0x7fffffffd6d0, x=...) at bigint.cpp:104
104
Bigint result;
(gdb) n
105
result.clear(); //make result being zero
(gdb) n
107
if(size < x.size)
(gdb) p result
$1 = {static MAX_SIZE = 500, size = -14560, arr = {32767, 0 <repeats 499 times>}}
(gdb) q

注意到clang++的这句了吗

(gdb) p result
$1 = {static MAX_SIZE = 500, size = -14560, arr = {32767, 0 <repeats 499 times>}}

我执行了result.clear()的意思的清空当前的result,使得size = 1, arr[]全为0,然而在clang++中居然不是这样

可以看到g++生成的文件中是正确的

(gdb) p result
$1 = {static MAX_SIZE = 500, size = 1, arr = {0 <repeats 500 times>}}

类的实现代码是这样的

void Bigint::clear(){
size = 1;
memset(arr, 0, sizeof(*arr) * MAX_SIZE);
}

不过最后我用gdb把个g++生成的文件调试好了,然后用clang++编译修改过的文件,再执行,居然结果也对,真是诡异


不过我有个疑问,是不是clang++生成的执行文件不能用gdb来调试呢?两者有冲突,clang++的有类似gdb的调试器?

clang++是刚刚安装的3.4版本,我的系统是ubuntu 13.10 x86_64



最后

以上就是优秀棒棒糖为你收集整理的clang++的诡异之处的全部内容,希望文章能够帮你解决clang++的诡异之处所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(50)

评论列表共有 0 条评论

立即
投稿
返回
顶部