Стандарт C++ 98 (1119566), страница 15
Текст из файла (страница 15)
The storage for these objects lasts until the block in which they are createdexits.2[Note: these objects are initialized and destroyed as described in 6.7. ]3If a named automatic object has initialization or a destructor with side effects, it shall not be destroyedbefore 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.3.7.3 Dynamic storage duration[basic.stc.dynamic]1Objects can be created dynamically during program execution (1.9), using new-expressions (5.3.4), anddestroyed using delete-expressions (5.3.5).
A C++ implementation provides access to, and management of,dynamic storage via the global allocation functions operator new and operator new[] and theglobal deallocation functions operator delete and operator delete[].2The library provides default definitions for the global allocation and deallocation functions.
Some globalallocation and deallocation functions are replaceable (18.4.1). A C++ program shall provide at most onedefinition of a replaceable allocation or deallocation function. Any such function definition replaces thedefault 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 programvoid* 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[].
[Note: the implicit declarations do not introduce thenames std, std::bad_alloc, and std::size_t, or any other names that the library uses to declarethese names. Thus, a new-expression, delete-expression or function call that refers to one of these functionswithout including the header <new> is well-formed. However, referring to std, std::bad_alloc, andstd::size_t is ill-formed unless the name has been declared by including the appropriate header. ]46© ISO/IEC3 Basic conceptsISO/IEC 14882:1998(E)3.7.3 Dynamic storage durationAllocation and/or deallocation functions can also be declared and defined for any class (12.5).3Any allocation and/or deallocation functions defined in a C++ program shall conform to the semantics specified in 3.7.3.1 and 3.7.3.2.3.7.3.1 Allocation functions[basic.stc.dynamic.allocation]1An allocation function shall be a class member function or a global function; a program is ill-formed if anallocation function is declared in a namespace scope other than global scope or declared static in globalscope.
The return type shall be void*. The first parameter shall have type size_t (18.1). The firstparameter shall not have an associated default argument (8.3.6). The value of the first parameter shall beinterpreted as the requested size of the allocation. An allocation function can be a function template. Sucha template shall declare its return type and first parameter as specified above (that is, template parametertypes shall not be used in the return type and first parameter type). Template allocation functions shall havetwo or more parameters.2The allocation function attempts to allocate the requested amount of storage.
If it is successful, it shallreturn the address of the start of a block of storage whose length in bytes shall be at least as large as therequested 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 apointer of any complete object type and then used to access the object or array in the storage allocated (untilthe storage is explicitly deallocated by a call to a corresponding deallocation function).
If the size of thespace requested is zero, the value returned shall not be a null pointer value (4.10). The results of dereferencing a pointer returned as a request for zero size are undefined.32)3An allocation function that fails to allocate storage can invoke the currently installed new_handler(18.4.2.2), if any. [Note: A program-supplied allocation function can obtain the address of the currentlyinstalled new_handler using the set_new_handler function (18.4.2.3).
] If an allocation functiondeclared with an empty exception-specification (15.4), throw(), fails to allocate storage, it shall return anull pointer. 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.4A global allocation function is only called as the result of a new expression (5.3.4), or called directly usingthe function call syntax (5.2.2), or called indirectly through calls to the functions in the C++ standardlibrary.
[Note: in particular, a global allocation function is not called to allocate storage for objects withstatic storage duration (3.7.1), for objects of type type_info (5.2.8), for the copy of an object thrown bya throw expression (15.1). ]3.7.3.2 Deallocation functions[basic.stc.dynamic.deallocation]1Deallocation 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 globalscope.2Each deallocation function shall return void and its first parameter shall be void*.
A deallocation function can have more than one parameter. If a class T has a member deallocation function named operatordelete with exactly one parameter, then that function is a usual (non-placement) deallocation function. Ifclass T does not declare such an operator delete but does declare a member deallocation functionnamed 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.
Similarly, if a class T has a member deallocationfunction 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 doesdeclare a member deallocation function named operator delete[] with exactly two parameters, the__________________32) The intent is to have operator new() implementable by calling malloc() or calloc(), so the rules are substantially thesame. C++ differs from C in requiring a zero request to return a non-null pointer.47ISO/IEC 14882:1998(E)3.7.3.2 Deallocation functions© ISO/IEC3 Basic conceptssecond of which has type std::size_t, then this function is a usual deallocation function.
A deallocation function can be an instance of a function template. Neither the first parameter nor the return type shalldepend on a template parameter. [Note: that is, a deallocation function template shall have a first parameterof type void* and a return type of void (as specified above). ] A deallocation function template shallhave two or more function parameters. A template instance is never a usual deallocation function, regardless of its signature.3The value of the first argument supplied to one of the deallocation functions provided in the standardlibrary may be a null pointer value; if so, the call to the deallocation function has no effect.
Otherwise, thevalue 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, conststd::nothrow_t&) in the standard library, and the value supplied to operatordelete[](void*) in the standard library shall be one of the values returned by a previous invocation ofeither operator new[](size_t) or operator new[](size_t, const std::nothrow_t&)in the standard library.4If the argument given to a deallocation function in the standard library is a pointer that is not the nullpointer 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 invalidpointer value (including passing it to a deallocation function) is undefined.33)3.7.4 Duration of sub-objects1[basic.stc.inherit]The storage duration of member subobjects, base class subobjects and array elements is that of their complete object (1.8).3.8 Object Lifetime1[basic.life]The lifetime of an object is a runtime property of the object. The lifetime of an object of type T beginswhen:— storage with the proper alignment and size for type T is obtained, and— if T is a class type with a non-trivial constructor (12.1), the constructor call has completed.The lifetime of an object of type T ends when:— if T is a class type with a non-trivial destructor (12.4), the destructor call starts, or— the storage which the object occupies is reused or released.2[Note: the lifetime of an array object or of an object of type (3.9) starts as soon as storage with proper sizeand alignment is obtained, and its lifetime ends when the storage which the array or object occupies isreused or released.
12.6.2 describes the lifetime of base and member subobjects. ]3The properties ascribed to objects throughout this International Standard apply for a given object only during its lifetime. [Note: in particular, before the lifetime of an object starts and after its lifetime ends thereare significant restrictions on the use of the object, as described below, in 12.6.2 and in 12.7. Also, thebehavior of an object under construction and destruction might not be the same as the behavior of an objectwhose lifetime has started and not ended. 12.6.2 and 12.7 describe the behavior of objects during the construction and destruction phases.