免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 3461 | 回复: 3

C/C++标准有没有规定未初始化的全局变量,装载时自动设为0? [复制链接]

论坛徽章:
0
发表于 2012-05-18 15:25 |显示全部楼层
10可用积分
如题。我知道全局变量有初始值的放在数据段,没有初始值的放在bss段。

但是C/C++标准有没有规定,装载bss大小的数据段的时候,全局变量所用的那些空间应该被默认初始化为全0?
谢谢。

最佳答案

查看完整内容

ISO C/C++没有.bss的概念。C的文件作用域对象,C++的全局(命名空间作用域)变量具有静态存储期:ISO C11(N1570)6.2.43 An object whose identifier is declared without the storage-class specifier _Thread_local, and either with external or internal linkage or with the storage-class specifier static, has static storage duration. Its lifetime is the entire execution of the program and its stored value is init ...

论坛徽章:
0
发表于 2012-05-18 15:25 |显示全部楼层
ISO C/C++没有.bss的概念。
C的文件作用域对象,C++的全局(命名空间作用域)变量具有静态存储期:
ISO C11(N1570)
6.2.4
3 An object whose identifier is declared without the storage-class specifier _Thread_local, and either with external or internal linkage or with the storage-class specifier static, has static storage duration. Its lifetime is the entire execution of the program and its stored value is initialized only once, prior to program startup.
ISO C++11
3.7.1 Static storage duration [basic.stc.static]
1 All variables which do not have dynamic storage duration, do not have thread storage duration, and are not local have static storage duration. The storage for these entities shall last for the duration of the program (3.6.2, 3.6.3).
如果没有显式初始化,则默认行为可以总结为填0(但关于实际的存储值则有些差别,例如null pointer不必然保证是0):
ISO C11(N1570) 6.7.9
10 If an object that has automatic storage duration is not initialized explicitly, its value is indeterminate. If an object that has static or thread storage duration is not initialized explicitly, then:
— if it has pointer type, it is initialized to a null pointer;
— if it has arithmetic type, it is initialized to (positive or unsigned) zero;
— if it is an aggregate, every member is initialized (recursively) according to these rules, and any padding is initialized to zero bits;
— if it is a union, the first named member is initialized (recursively) according to these rules, and any padding is initialized to zero bits;
关于ISO C++11见http://bbs.chinaunix.net/thread-3710317-4-1.html的40L。

论坛徽章:
14
巨蟹座
日期:2013-11-19 14:09:4615-16赛季CBA联赛之青岛
日期:2016-07-05 12:36:0515-16赛季CBA联赛之广东
日期:2016-06-29 11:45:542015亚冠之全北现代
日期:2015-07-22 08:09:472015年辞旧岁徽章
日期:2015-03-03 16:54:15巨蟹座
日期:2014-12-29 08:22:29射手座
日期:2014-12-05 08:20:39狮子座
日期:2014-11-05 12:33:52寅虎
日期:2014-08-13 09:01:31巳蛇
日期:2014-06-16 16:29:52技术图书徽章
日期:2014-04-15 08:44:01天蝎座
日期:2014-03-11 13:06:45
发表于 2012-05-18 15:36 |显示全部楼层
1。C/C++ 估计没有所谓 bbs 等概念
2。全局变量默认初始化的,因为没法保证赋值在存取之前完成

论坛徽章:
59
2015年亚洲杯之约旦
日期:2015-01-27 21:27:392015年亚洲杯之日本
日期:2015-02-06 22:09:41拜羊年徽章
日期:2015-03-03 16:15:432015年辞旧岁徽章
日期:2015-03-03 16:54:152015年迎新春徽章
日期:2015-03-04 09:50:282015元宵节徽章
日期:2015-03-06 15:50:392015年亚洲杯之阿联酋
日期:2015-03-19 17:39:302015年亚洲杯之中国
日期:2015-03-23 18:52:23巳蛇
日期:2014-12-14 22:44:03双子座
日期:2014-12-10 21:39:16处女座
日期:2014-12-02 08:03:17天蝎座
日期:2014-07-21 19:08:47
发表于 2012-05-18 20:57 |显示全部楼层
no, os do it....
the bss section store the variants that its initialize value is not 0.
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP