C++primer the 19th Chapter

Source: Internet
Author: User

//1. Control memory allocation://A: Some applications have special requirements for memory allocation, so we cannot apply the standard memory management mechanism directly to these programs. They often require custom memory allocation details, such as using the keyword new to place objects in a specific memory space. //B: The following code:string*SP =New string("s");string*arr =New string[Ten];//the new series operator actually performs a three-step operation://The first step: The new expression invokes a standard library function called operator new (or operator new[]). This function allocates a large enough primitive, non-constructed memory space. //The second step: The compiler runs the appropriate constructors to construct the objects and pass them the initial values. //Step Three: The object is allocated space and finished constructing, returning a pointer to the object. Deletesp;Delete[]arr;//the Delete series operator actually performs two steps://The first step is to perform the corresponding destructor on the object that the SP refers to or the elements in the array that Arr refers to. //Step Two: The compiler calls the standard library function named operator delete (or operator delete[]) to free up space. //If your program wants to control the memory allocation process, you need to define the operator new function and the operator delete function yourself. The lookup process for these two functions is: first find in the class space of the corresponding class (if the object of the operation is a class type), second in the global space, and finally the version of the standard library is called. //C: The standard library defines the operator new series functions and operator delete series functions, where nothrow_t is the version that does not throw exceptions:_RET_BYTECAP_ (_size)void*__crtdecloperator New(size_t _size) _throw1 (_std bad_alloc); _ret_bytecap_ (_size)void*__crtdecloperator New[] (size_t _size); _ret_opt_bytecap_ (_size)void*__crtdecloperator New(size_t _size,Const_STD nothrow_t&); _ret_opt_bytecap_ (_size)void*__crtdecloperator New[] (size_t _size,Const_STD nothrow_t&);void__crtdecloperator Delete(void*) _throw0 ();void__crtdecloperator Delete[](void*) _throw0 ();void__crtdecloperator Delete(void*,Const_STD nothrow_t&);void__crtdecloperator Delete[](void*,Const_STD nothrow_t&);structnothrow_t{//placement new tag type to suppress exceptions};extern Constnothrow_t nothrow;//constant for placement new tag//the application can customize any of these versions, provided that the custom version must be in a global scope or a class scope. When the above function is defined as a member function of a class, it is implicitly static because the operator new series function is used before the object is constructed, and the operator delete series function is used after the object is destroyed, so such a function cannot manipulate the data members of the class. //for operator new series functions, their return value must be void*, the first parameter type must be size_t and cannot have a default argument, which is used to hold the number of bytes required to construct the memory. //If you want to customize the operator new series functions, you can provide them with additional parameters. At this point, the new expression used for these custom functions must pass the arguments to the new parameter using the positional form of new. void*__crtdecloperator New(size_t _size,intnvalue) {printf ("%d%d\n", _size, Nvalue); return malloc(4);}int*p =New( -)int(Ten);//p = 0x000e36b0 *p = 10; output 4//D: Although under normal circumstances we can customize operator new with any formal parameters, the following function cannot be overloaded by the user anyway:Inlinevoid*__crtdecloperator New(size_t,void*_where) _throw0 () {//construct array with placement at _where    return(_where);} Inlinevoid*__crtdecloperator New[] (size_t,void*_where) _throw0 () {//construct array with placement at _where    return(_where);} Inlinevoid__crtdecloperator Delete(void*,void*) _throw0 () {//Delete if placement new fails}inlinevoid__crtdecloperator Delete[](void*,void*) _throw0 () {//Delete if placement array new fails}//the operator new and operator delete series functions described above are intended for use by standard libraries, so they should not be redefined//E: For operator delete series functions, their return value type must be void and the first parameter must be the void* type. Executing a delete expression invokes the corresponding operator function and initializes the void* parameter with the pointer to be released. //When we define a function of the operator delete series as a member of a class, the function can contain another parameter of type size_t. At this point, the initial value of the parameter is the number of bytes of the object that the first parameter refers to. The size_t parameter can be used to delete an object from an inherited system. //The destructors class has a virtual destructor, the number of bytes passed to the operator delete series function differs depending on the dynamic type of the object to be deleted, and the version of the operator delete function that is actually running will change. classcfather{ Public: Cfather () {}Virtual~cfather () {printf ("0\n");} Public:    void operator Delete(void*p, size_t size) { Free(p); printf"1%d\n", size);} Public:    intvalue;};classCchild: Publiccfather{ Public: Cchild () {}~cchild () {printf ("2\n");} Public:    intValue0; intvalue1; Public:    void operator Delete(void*p, size_t size) {printf ("3%d\n", size); Free(P);}; Cchild*pchild =NewCchild; Cfather*ptem =pchild;DeletePtem;//Output 2 0 3//F: We provide operator new AND operator delete series functions to change the way memory is allocated, but we cannot change the basic meaning of the new operator and the delete operator anyway. //G: Use malloc and free (all defined in the header file Cstdlib) in the operator new series functions and operator delete series functions that you define to complete the allocation and deallocation of memory. //H: Position new Form (contains header file New): The version of new that cannot be redefined is called earlierint*pint =New int(Ten);//Call Standard library version pInt = 0x00251510 *pint=int*pint1 =New(PINT)int( -);//Call Standard library version pInt1 = 0x00251510 *pint1 = *pint =//when called only through an address, the location new uses operator new (size_t, void*); The function does not allocate any memory, just returns a pointer argument. The specific definition and its corresponding delete are specified in clause d. //in fact, positioning new allows us to construct objects on a specific, pre-allocated memory. This piece of memory may not even be dynamic memory. classctest{ Public:    ~ctest () {printf ("0\n");}}; CTest*test0 =NewCTest; Test0->~ctest ();//calling destructors destroys objects but does not free memoryCTest *test1 =New(TEST0) CTest;//Reusing memoryDeleteTest1;//Freeing Memory

C++primer the 19th Chapter

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.