C + + supplements--new Delete overload

Source: Internet
Author: User

C + + supplements--new Delete overload

Preface

New and delete are a pair of operations that manipulate dynamic memory. Overloading them allows for efficient customization of memory management.

Body1. Partial overloading

For a particular type, the new and delete overloads can be monitored for dynamic creation of that type of object. The following code:

Codea

#include <iostream>using namespace Std;class myclass{public:myclass () {cout << "MyClass ()" << Endl;} ~myclass () {cout << "~myclass ()" << Endl;} void *operator New (std::size_t size) {cout << "local new call" << endl;void *mem = malloc (size);    if (MEM)     //memory allocation fails, returns 0return malloc (size); Elsethrow bad_alloc ();     Memory allocation failed, throwing exception}void operator delete (void *ptr) {cout << "local Delete call" << endl;//is not empty, then calling free frees memory if (PTR) { Free (PTR);}}; int main () {cout << "****** local new Delete overload demo ***by david***" << Endl; MyClass *my = new Myclass;delete my;cin.get (); return 0;}
Run



The running results show that

The expression new incorporates memory allocations and constructors. It first calls malloc to allocate memory, and then invokes the specified type and matches the constructor to initialize that segment of memory.

The expression delete consolidates the destructor and memory deallocation. The destructor of the class is called first to release the resource, and then free to release the allocated memory.


Code two

The following example provides a way to monitor memory allocations.

#include <iostream>using namespace Std;class Myclass{public://count records the number of objects not released static int count;int A; MyClass () {cout << "MyClass ()" << endl;count++;} ~myclass () {cout << "~myclass ()" << endl;count--;}    New local overload void *operator new (size_t size) {cout << "local new call" << endl;void *mem = malloc (size);     Memory allocation failed, return 0if (MEM) return malloc (size); Elsethrow bad_alloc (); Memory allocation failed, throw exception}//new[] local overload void *operator new[] (std::size_t size) {cout << "local new[] call" << endl;void *mem = ma    Lloc (size);     Memory allocation failed, return 0if (MEM) return malloc (size); Elsethrow bad_alloc (); Memory allocation failed, throw exception}//delete partial reload void operator delete (void *ptr) {cout << "local Delete call" << endl;//not empty, The call to free frees memory if (PTR) {a (PTR);}} delete[] Local overload void operator delete[] (void *ptr) {cout << "local delete[] call" << endl;//ptr is not empty, then calling free frees memory if ( PTR) {free (PTR);}}; int myclass::count = 0;int Main () {cout << "******new Delete local overload demo ***by david***" << endl;cout << "Start Myclass::count =" << myclass::count << Endl; MyClass *my = new Myclass;delete my;cout << "-----------------" << Endl; MyClass my1;cout << "-----------------" << Endl; MyClass *mys = new Myclass[5];cout << "Myclass::count =" << myclass::count << endl;delete[]mys;cout &lt ;< "Myclass::count =" << myclass::count << endl;cin.get (); return 0;}
Run



2. Global overloading

The new and delete overloads of the global can monitor all types of memory allocations.

#include <iostream> #include <string>using namespace Std;class myclass{public:myclass () {cout << " MyClass () "<< Endl;} ~myclass () {cout << "~myclass ()" << Endl;}  void *operator New (std::size_t size) {cout << myclass::new overload << endl;void *mem = malloc (size); if (MEM) return Mem;elsethrow bad_alloc ();} void *operator new[] (std::size_t size) {cout << myclass::new[] Overloads << endl;void *mem = malloc (size); if (MEM) re Turn Mem;elsethrow bad_alloc ();} void operator delete (void *ptr) {cout << MyClass::d elete Reload << endl;if (PTR) {free (PTR);}} void operator delete[] (void *ptr) {cout << MyClass::d elete[] overload << endl;if (PTR) {free (PTR);}}};/ /Global New overload void *operator new (std::size_t size) {cout << Global new overload << endl;void *mem = malloc (size); if (MEM) retur n Mem;elsethrow bad_alloc ();} Global new[] overloaded void *operator new[] (std::size_t size) {cout << global new[] Overloads << endl;void *mem = malloc (size); if (Me m) return Mem;elsethrow BAD_alloc ();} Global delete overload void operator delete (void *ptr) {cout << global delete overload << Endl;if (PTR) {free (PTR);}} Global delete[] overloaded void operator delete[] (void *ptr) {cout << global delete[] overload << endl;if (PTR) {free (PTR);}} int main () {cout << ****** global/local new and delete are overloaded ***by david*** << endl;int *p = new Int;delete p;cout << "-------------------" << endl;double *ds = new Double[10];d elete[]ds;cout << "-------------------" << Endl MyClass *my = new Myclass;delete my;cout << "-------------------" << Endl; MyClass *mys = new Myclass[3];d elete[]mys;cin.get (); return 0;}
Run



If the type redefined new and delete, the call is local, otherwise the global is called.


Details

    • The return type of operator new or operator new[] must be void*.
    • The return type of operator delete or operator delete[] must be void.
    • Both the new and delete overloads in the class are implicitly static. If you explicitly declare it, you will not get an error.
    • size_t is unsigned int. When the compiler calls operator new, the number of bytes required to store the specified type of object is passed to the size_t parameter. When operator new[] is called, the number of bytes of all elements in the array is passed.




Directory of this column

    • C + + Supplements directory

Directory of all content

    • CCPP Blog Directory




C + + supplements--new Delete overload

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.