C语言标准6.2.4/5/6阅读笔记

本文探讨了在C/C++中,对象与变量长度数组(VLA)的生命周期及其初始化特性。阐述了对象的生命周期从进入相关块开始,直到块以任何方式结束,以及VLA类型对象的生命周期从声明到程序离开声明范围。重点分析了初始化对于确定对象值的重要性,以及VLA对象在每次声明时因未初始化而导致的不确定性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

5 For such an object that does not have a variable length array type, its lifetime extends
from entry into the block with which it is associated until execution of that block ends in
any way. (Entering an enclosed block or calling a function suspends, but does not end,
execution of the current block.) If the block is entered recursively, a new instance of the
object is created each time.
The initial value of the object is indeterminate. If an
initialization is specified for the object, it is performed each time the declaration is
reached in the execution of the block; otherwise, the value becomes indeterminate each
time the declaration is reached

就是说,如果重复递归的进入这个block,那么就会生成这个object的一个实例。如果每次都声明并初始化一个值给这个object实例,那么这个object的值就是确定的,否则就是不确定的。那么进入这个block的方式通常有:

第一种:通过函数调用反复的进入一个block,这个block肯定每次都会执行初始化语句,那么值必然是确定的。

第二种:通过循环不断进入一个block,那么每次也会执行初始化语句,那么值必然是确定的。

第三种:通过goto这种跳转语句不断进入一个blcok,由于跳转的位置,可能不会执行block中的初始化语句,而直接使用object这个实例,那么值是不确定的。

总之,一句话,一个对象只要初始化了,那么值就是确定的,如果没有初始化,那么值就是不确定的。

 

6 For such an object that does have a variable length array type, its lifetime extends from
the declaration of the object until execution of the program leaves the scope of the
declaration.27) If the scope is entered recursively, a new instance of the object is created
each time. The initial value of the object is indeterminate

 

关于VLA类型的object,该类型在声明的时候不能进行初始化,所以就有一个问题,每次声明生成一个该对象的实例,由于没有进行初始化,所以其值是不确定的。

这也就是在上面第六点所说的,VLA类型的object,如果反复的进入一个block,那么该object实例的值是不确定的,因为声明的时候无法同时初始化。

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值