- 论坛徽章:
- 17
|
回复 1# 801901987
你没有理解作用域和生存周期的问题,int是scalar type,scalar type对象的赋值都是by value的方式。比如说return c;那么c的值就copy到了函数的返回值中,函数退出后c就不存在,这时没有关系因为c的值已经copy到内存中函数的返回值中了。C标准规定的存储周期类型只有三种:static,automatic,allocated。存储周期类型决定对象的生命周期(An object has a storage duration that determines its lifetime. There are three storage
durations: static, automatic, and allocated.),全局变量的存储类型为static,普通局部变量的存储类型为automatic。存储类型为static的对象在main函数(The function called at program startup is named main.)调用前进行初始化(All objects with static storage duration shall be initialized (set to their initial values) before program startup.),automatic类型的变量的生存周期仅限于包含它的块(A block with initialization of an object that has automatic storage duration),当执行进入代码块时初始化局部变量,在执行完代码块时销毁局部变量。 |
|