- 論壇徽章:
- 17
|
回復(fù) 1# 801901987
你沒有理解作用域和生存周期的問題,int是scalar type,scalar type對象的賦值都是by value的方式。比如說return c;那么c的值就copy到了函數(shù)的返回值中,函數(shù)退出后c就不存在,這時(shí)沒有關(guān)系因?yàn)閏的值已經(jīng)copy到內(nèi)存中函數(shù)的返回值中了。C標(biāo)準(zhǔn)規(guī)定的存儲周期類型只有三種: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函數(shù)(The function called at program startup is named main.)調(diào)用前進(jìn)行初始化(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),當(dāng)執(zhí)行進(jìn)入代碼塊時(shí)初始化局部變量,在執(zhí)行完代碼塊時(shí)銷毀局部變量。 |
|