Automatic Objects and Local static Objects

来源:互联网 发布:金和网络财报2017 编辑:程序博客网 时间:2024/06/11 00:25

Automatic Objects

The objects that correspond to ordinary local variables are created when the function's control path passes through the variable's defintion. They are destroyed when control passes through the end of the block in which the variable is defined.Objects that exist only while a block is executing are known as automatic objects. After execution exits a block, the values of the automatic objects created in that block are undefined.

Parameters are automatic objects. Storage for the parameters is allocated when the function begins. Parameters are defined inthe scope of the function body. Hence they are destroyed when the function terminates.

Automatic objects corresponding to the function's parameters are initialized by the arguments passed to the function. Automatic objects corresponding to local variables are initialized if their definition contains an initializer. Otherwise, they are default initialized, which means that uninitialized local variables of built-in type have undefined values.

Local static Objects

It can be useful to have a local variable whose lifetime continues across calls to the function. We obtain such objects by defining a local variable as static. Each local static object is initialized before the first time execution passes through the object's definition. Local statics are not destroyed when a function ends; they are destroyed when the program terminates.

... ...

If a local static has no explicit initializer, it's value initialized, meaning that local statics of built-in type are initialized to zero.

0 0
原创粉丝点击