免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
楼主: wzlxx_
打印 上一主题 下一主题

gcc是否为我们做的太多 [复制链接]

论坛徽章:
0
11 [报告]
发表于 2010-06-07 21:18 |只看该作者
拷贝构造函数?
那是g++吧。

论坛徽章:
2
青铜圣斗士
日期:2015-11-26 06:15:59数据库技术版块每日发帖之星
日期:2016-07-24 06:20:00
12 [报告]
发表于 2010-06-07 21:57 |只看该作者
不但没有警告而且运行没事,哈哈哈哈,GCC太智能了。回复  xinglp
wzlxx_ 发表于 2010-06-07 19:50


我猜不是gcc智能的原因。
gcc应该生成的是和vc6差不多的代码, 即生成的复制构造仅仅是member-wise拷贝。依然会导致多次free的行为
lz可以做个实验看看, 分析问题究竟是:gcc太智能, 没有产生多重free的行为;还是glibc太溺爱, 即使多重free也没有立即崩溃。

论坛徽章:
0
13 [报告]
发表于 2010-06-07 21:59 |只看该作者
我倒是没看出来一个类没有拷贝构造函数在编译期究竟应该出什么问题。

难道你还要编译器这么说——警告:XXX 类没有拷贝构造函数?

论坛徽章:
0
14 [报告]
发表于 2010-06-07 22:17 |只看该作者
回复 12# OwnWaterloo


    我就是忘记了这一点才弄看了半天代码没有发现呢,G++编译过的可执行文件似乎没有多次free,程序运行的很正常,不过潜在的问题我不知道。不过同样的代码在VC6.0根本无法执行

论坛徽章:
0
15 [报告]
发表于 2010-06-07 22:20 |只看该作者
回复 13# 变异老鼠

不是说编译器会提示,因为这个是运行时错误,但是在VC6.0下生成的执行文件无法运行,因为多次free造成了很多数据无法访问,但在linux下生成的暂时没有发现问题

论坛徽章:
0
16 [报告]
发表于 2010-06-08 09:25 |只看该作者
你去用VC2.x去吧。

论坛徽章:
1
2015年辞旧岁徽章
日期:2015-03-03 16:54:15
17 [报告]
发表于 2010-06-08 09:37 |只看该作者
vc6再不济,gcc和vc这点上的行为也应该是一样的。

论坛徽章:
0
18 [报告]
发表于 2010-06-08 10:24 |只看该作者
By default, class objects can be copied. In particular, a class object can be initialized with a copy of another object of the same class. This can be done even where constructors have been declared.

By default, the copy of a class object is a copy of each member. If that default is not the behavior wanted for a class X, a more appropriate behavior can be provided by defining a copy constructor, X::X(const X&).


By Bjarne Stroustrup

If the class definition does not explicitly declare a copy constructor and there is no user-declared move
constructor, a copy constructor is implicitly declared as defaulted (8.4). Thus, for the class definition
struct X { X(const X&, int);
};
a copy constructor is implicitly-declared. If the user-declared constructor is later defined as
X::X(const X& x, int i =0) { /∗ ... ∗/ }
then any use of X’s copy constructor is ill-formed because of the ambiguity; no diagnostic is required.

12.8        Copying and moving class objects, Programming Languages — C++, ISO/IEC JTC1 SC22 WG21 N 3092

论坛徽章:
0
19 [报告]
发表于 2010-06-08 11:29 |只看该作者
这个gcc vc 都没关系吧

本来就是默认的复制构造函数


应该是glibc 好 没让你马上挂。  我这里有个bug 开发机从来不挂 测试机 直接挂

论坛徽章:
2
摩羯座
日期:2013-10-10 14:29:04天蝎座
日期:2014-01-03 09:14:49
20 [报告]
发表于 2010-06-08 11:32 |只看该作者
我们为gcc做得太少
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP