C++03标准阅读笔记-3.7 Storage duration

3.7 Storage duration 存储周期 
1 Storage duration is the property of an object that defines the minimum potential lifetime of the storage containing the object. 
存储周期(也叫存储持续性)指包含该对象的存储可能的最小生命周期。
The storage duration is determined by the construct used to create the object and is one of the following:
存储周期是创建对象的构造决定的,它的类型有以下三种:
— static storage duration
静态存储周期
— automatic storage duration
自动存储周期
— dynamic storage duration
动态存储周期
2 Static and automatic storage durations are associated with objects introduced by declarations (3.1) and implicitly created by the implementation (12.2).The dynamic storage duration is associated with objects created with operator new (5.3.4).
静态和动态存储周期对象由声明和实现隐式创建。动态存储周期对象由操作符new创建。
3 The storage class specifiers static and auto are related to storage duration as described below.
存储类指定符static和auto与存储周期关联在下面描述。
4 The storage duration categories apply to references as well. The lifetime of a reference is its storage duration.
存储周期分类也适应于引用。引用的生命周期即是它的存储周期。

3.7.1 Static storage duration 静态存储周期
1 All objects which neither have dynamic storage duration nor are local have static storage duration. The storage for these objects shall last for the duration of the program (3.6.2, 3.6.3).
所有对象即不是动态也不是本地对象,那它们就是静态存储对象。这些对象在程序运行期一直存在。
2 If an object of static storage duration has initialization or a destructor with side effects, it shall not be eliminated even if it appears to be unused, except that a class object or its copy may be eliminated as specified in 12.8.
如果一个静态存储对象初始化和销毁带有副作用,就算它没有使用也不能消除,除非按照12.8所定义才能消除它或它的副本。

3 The keyword static can be used to declare a local variable with static storage duration. [Note: 6.7 describes the initialization of local static variables; 3.6.3 describes the destruction of local static variables. ] 
关键字static能够声明一个本地的静态存储周期对象。
4 The keyword static applied to a class data member in a class definition gives the data member static storage duration.
关键字static应用在类数据成员中,那数据成员为静态存储周期。

3.7.2 Automatic storage duration 自动存储周期
1 Local objects explicitly declared auto or register or not explicitly declared static or extern have automatic storage duration. The storage for these objects lasts until the block in which they are created exits.
本地对象显示声明为auto或register或没有显式声明为static或extern都为自动存储周期对象。这些对象存储周期最后为在退出创建它们的代码块。
2 [Note: these objects are initialized and destroyed as described in 6.7. ]
这些对象初始化和销毁在6.7节描述。
3 If a named automatic object has initialization or a destructor with side effects, it shall not be destroyed before the end of its block, nor shall it be eliminated as an optimization even if it appears to be unused, except that a class object or its copy may be eliminated as specified in 12.8.
如果一个命名的对象实始化或析构带有副作用,它不能在它所在块的最之前销毁,就算它未被使用也不能被优化消除掉,除非按照12.8所定义才能消除它或它的副本。

3.7.3 Dynamic storage duration 动态存储周期
1 Objects can be created dynamically during program execution (1.9), using new-expressions (5.3.4), and destroyed using delete-expressions (5.3.5). 
动态存储周期对象在程序运行期间使用new创建对象,使用delete销毁对象。
A C++ implementation provides access to, and management of, dynamic storage via the global allocation functions operator new and operator new[] and the global deallocation functions operator delete and operator delete[].
C++实现通过提供全局new和new[]分配操作符,delete和delete[]释放操作符访问,管理动态存储周期对象。
2 The library provides default definitions for the global allocation and deallocation functions. Some global allocation and deallocation functions are replaceable (18.4.1). A C++ program shall provide at most one definition of a replaceable allocation or deallocation function. 
库提供默认的全局分配和释放函数。一些全局分配和释放函数能够替换。C++程序最多应该只提供一个分配和释放替换函数。
Any such function definition replaces the default version provided in the library (17.4.3.4). 
任何这样的函数定义都会替换库中提供的默认版本。
The following allocation and deallocation functions (18.4) are implicitly declared in global scope in each translation unit of a program 
以下分配和释放函数隐式定义在每一个编译单元会局范围内。
void* operator new(std::size_t) throw(std::bad_alloc);
void* operator new[](std::size_t) throw(std::bad_alloc);
void operator delete(void*) throw();
void operator delete[](void*) throw();

These implicit declarations introduce only the function names operator new, operator new[], operator delete, operator delete[]. 
这些隐式声明只引入操作符new,new[],delete和delete[]。
[Note: the implicit declarations do not introduce the names std, std::bad_alloc, and std::size_t, or any other names that the library uses to declare these names. 
 Thus, a new-expression, delete-expression or function call that refers to one of these functions without including the header <new> is well-formed. However, referring to std, std::bad_alloc, and std::size_t is ill-formed unless the name has been declared by including the appropriate header. ]
因此,一个new表达式,delete表达式或这些函数调用可以不包含头文件<new>。但是,直接引用std,std::bad_alloc和std::size_t是非法的,除非包含有声明它们的头文件。
Allocation and/or deallocation functions can also be declared and defined for any class (12.5).
分配和/或释放函数能在任何类中声明和定义。
3 Any allocation and/or deallocation functions defined in a C++ program, including the default versions in the library, shall conform to the semantics specified in 3.7.3.1 and 3.7.3.2.
任何定义在C++程序中的分配和/或释放函数,包括在库中定义的默认版本,都必须依照3.7.3.1和3.7.3.2中指定的语法。
3.7.3.1 Allocation functions 分配函数
1 An allocation function shall be a class member function or a global function; a program is ill-formed if an allocation function is declared in a namespace scope other than global scope or declared static in global scope. 
分配函数必须是类成员函数或全局函数;如果分配函数声明不是在全局的命名空间内,或在全局范围内声明为静态,那它是非法的。
The return type shall be void*. The first parameter shall have type size_t (18.1). The first parameter shall not have an associated default argument (8.3.6). The value of the first parameter shall be interpreted as the requested size of the allocation. 
返回类型必须为void*。第一个参数类型必须为size_t。第一个参数必须不能是默认的参数。第一个参数的值必须解释为分配需求的大小。
An allocation function can be a function template. Such a template shall declare its return type and first parameter as specified above (that is, template parameter types shall not be used in the return type and first parameter type). Template allocation functions shall have two or more parameters.
分配函数可以是模板函数,模板函数声明它的返回类型和第一个参数必须与上面要求相同。模板分配函数可以两个或更多的参数。
2 The allocation function attempts to allocate the requested amount of storage. If it is successful, it shall return the address of the start of a block of storage whose length in bytes shall be at least as large as the requested size. 
分配函数尝试分配需要的存储空间。如果成功,它返回存储块的开始地址,存储块最少为请求字节的大小。
There are no constraints on the contents of the allocated storage on return from the allocation function. 
从分配函数返回的存储块没有包含任何内容。
The order, contiguity, and initial value of storage allocated by successive calls to an allocation function is unspecified. 
连接调用分配函数所分配的存储空间,它们的顺序,连续性和初始化未指定。
The pointer returned shall be suitably aligned so that it can be converted to a pointer of any complete object type and then used to access the object or array in the storage allocated (until the storage is explicitly deallocated by a call to a corresponding deallocation function). 
返回的旨针应当适当对象,以便能够转换为任何完整类型的对象,及使用它们访问分配存储空间中的对象和数组。
Even if the size of the space requested is zero, the request can fail. If the request succeeds, the value returned shall be a nonnull pointer value (4.10) p0 different from any previously returned value p1, unless that value p1 was subsequently passed to an operator delete. 
即使请求的空间大小为0,也可能失败。如果请请求成功,它的返回值必须是个非null指针p0,与前面的返回值p1不同,除非在后续p1传给操作符delete。
The effect of dereferencing a pointer returned as a request for zero size is undefined.
取消一个请求为0的返回指针的影响是未定义的。
3 An allocation function that fails to allocate storage can invoke the currently installed new_handler (18.4.2.2), if any. 
如果有安装new_hanler,则分配失败将调用当前安装的new_handler。
[Note: A program-supplied allocation function can obtain the address of the currently installed new_handler using the set_new_handler function (18.4.2.3). ] 
If an allocation function declared with an empty exception-specification (15.4), throw(), fails to allocate storage, it shall return a null pointer. 
如果分配函数声明为空的异常规范,throw(),失败时将返回null指针。
Any other allocation function that fails to allocate storage shall only indicate failure by throwing an exception of class std::bad_alloc (18.4.2.1) or a class derived from std::bad_alloc.
如果分配函数失败,必须抛出类std::bad_alloc或它的继续类的表达式。
4 A global allocation function is only called as the result of a new expression (5.3.4), or called directly using the function call syntax (5.2.2), or called indirectly through calls to the functions in the C++ standard library. 
[Note: in particular, a global allocation function is not called to allocate storage for objects with static storage duration (3.7.1), for objects of type type_info (5.2.8), for the copy of an object thrown by a throw expression (15.1). ]
3.7.3.2 Deallocation functions 释放函数
1 Deallocation functions shall be class member functions or global functions; a program is ill-formed if deallocation functions are declared in a namespace scope other than global scope or declared static in global
scope.
释放函数必须是类成员函数或全局函数,如果释放函数声明在非全局命名空间内或声明全局静态函数,则它是非法的。
2 Each deallocation function shall return void and its first parameter shall be void*. A deallocation function can have more than one parameter. 
每一个释放函数都必须返回void并且它的第一个参数必须是void*。释放函数可以参数个数可以大于1。
If a class T has a member deallocation function named operator delete with exactly one parameter, then that function is a usual (non-placement) deallocation function.If class T does not declare such an operator delete but does declare a member deallocation function named operator delete with exactly two parameters, the second of which has type std::size_t (18.1), then this function is a usual deallocation function.
如果类T有一个释放成员函数操作符delete只有一个参数,那么这个函数是个常用的释放函数。如果类T没有声明像上面的delete操作符,但有声明两个参数的delete[]操作符函数,第二个参数类型为std::size_t,那么这个函数是常用的释放函数。
Similarly, if a class T has a member deallocation function named operator delete[] with exactly one parameter, then that function is a usual (nonplacement) deallocation function. If class T does not declare such an operator delete[] but does declare a member deallocation function named operator delete[] with exactly two parameters, the second of which has type std::size_t, then this function is a usual deallocation function. 
类似的,如果类T有一个释放成员函数操作符delete[]只有一个参数,那么这个函数是个常用函数。如果类T没有声明像上面的delete[]操作符,但有声明两个参数的delete[]操作符函数,第二个参数类型为std::size_t,那么这个函数是常用的释放函数。
A deallocation function can be an instance of a function template. Neither the first parameter nor the return type shall depend on a template parameter. 
释放函数可以是函数模板的一个实例。 第一个参数和返回类型都不依赖于模板参数。
[Note: that is, a deallocation function template shall have a first parameter of type void* and a return type of void (as specified above). ] 
A deallocation function template shall have two or more function parameters. A template instance is never a usual deallocation function, regardless of its signature.
释放函数模板必须有两个或更多参数。一个模板实例不是常用的释放函数,不管它的函数声明是什么。
3 The value of the first argument supplied to one of the deallocation functions provided in the standard library may be a null pointer value; if so, the call to the deallocation function has no effect.
提供给标准库释放函数的第一个参数值可以为空指针,如果是这样的话,调用释放函数没有任何影响。
Otherwise, the value supplied to operator delete(void*) in the standard library shall be one of the values returnedby a previous invocation of either operator new(size_t) or operator new(size_t, const std::nothrow_t&) in the standard library, and the value supplied to operator delete[](void*) in the standard library shall be one of the values returned by a previous invocation of either operator new[](size_t) or operator new[](size_t, const std::nothrow_t&) in the standard library.
否则,标准库中操作符delete(void*)的参数值必须是之前调用标准库中操作符new(size_t)或new(size_t, const std::nothrow_t&)返回的值,并且标准库中的操作符delete[](void*)的参数值必须是之前调用标准库中操作符new[](size_t)或new[](size_t, const std::nothrow_t&)返回的值。
4 If the argument given to a deallocation function in the standard library is a pointer that is not the null pointer value (4.10), the deallocation function shall deallocate the storage referenced by the pointer, rendering invalid all pointers referring to any part of the deallocated storage. The effect of using an invalid pointer value (including passing it to a deallocation function) is undefined.33)
如果提供给标准库的参数值为非空指针,释放函数释放指针所指向的存储空间,所有指向释放存储空间任何部分的指针都变为无效。使用一个无效的指针值是未定义的。
3.7.4 Duration of sub-objects 子对象的存储周期
1 The storage duration of member subobjects, base class subobjects and array elements is that of their complete object (1.8).
子对象成员、基类子对象和数组元素的存储周期,是它们是完整对象的存储周期。
 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值