The thinking and exploration of malloc free and New,delete--1

Source: Internet
Author: User

/********************************************************************** * * Copyright (c) 2015,wk Studios * * Fil ENAME:A.H * * COMPILER:GCC VC 6.0 * * AUTHOR:WK * * time:2015 25 7 * ********************************* #include <iostream>using namespace Std;/*//malloc and the free function, the new is built with sizeof Type conversions, and type safety checks for non-intrinsic type objects are dynamically created while the object is initialized void* malloc (size_t size)//Here is the number of bytes {}void free (void *memblock)// The type of the pointer and the capacity in memory are known beforehand that for a null pointer can be freed countless times, for a non-null pointer to be released only once {//free () does not release the contents of the pointer into a random value, but instead of the address stored in the pointer that corresponds to the block within the The save and release becomes random, so you cannot access the data in the memory at this time, but you can find the memory through the pointer, just read the contents of it is illegal}//plain new/delete, standard C + + in the application fails to throw a standard exception, Therefore, the exception handler must be set at the time of Use void* operator new (size_t lenth) throw (std::bad_alloc) {}void operator delete (void* memblock) throw () { }//nothrow New/delete, the application fails to return NULL when used without setting the exception handler void* operator new (size_t lenth,const std::nothrow_t &ex) Throw () {}void operator delete (void* memblock) throw ();//placement new/deletevoid* _operAtor New (size_t void* mem) {} void _operator Delete (void* mem,void* bloc) {}*///nothrow new/delete using void func (size_t l Enth) {unsigned char *p = new (nothrow) unsigned char[lenth];if (p = = NULL) {cout<< "Allocate failed!! \ n "; exit (-1);} delete []p;} Plain New/delete uses char * getmemory (unsigned long lenth) {char *p = new Char[lenth];return p;} Class base{public:base (int a=0): M_a (a) {}~base () {}private:int m_a;}; void Main () {//malloc free uses long *buffer;long *buffer1;buffer1= (long *) malloc (40*sizeof (long));//Space random value if (buffer1! = N  ULL) printf ("Allocated integers\n"); elseprintf ("Can ' t allocate memory\n"), buffer = (long *) calloc (max, sizeof ( Long));//Space all set 0if (buffer! = NULL) printf ("Allocated integers\n"); elseprintf ("Can ' t allocate memory\n"); fre  E (buffer); free (buffer1); try{char *p = getmemory (100000000); delete []p;} catch (const std::bad_alloc& ex) {cout<<ex.what () <<endl;} catch (...) {cout<< "ERROR!!!  \ n ";} Func (+);//placement NewTo reconstruct an object or an array of objects on top of the memory that has been successfully allocated, do not worry about memory allocation failure, because locating new does not allocate memory just call the constructor to create the object char*p = new (nothrow) char[sizeof (base)];// Nothrow newif (p = = NULL) {cout<< "Allocate failed!! \ n "; exit (-1);} Base *q = new (p) base (n),//placement new Q->~base ();//Call destructor to be displayed delete []p;char *PB = new (Nothrow) char[sizeof (in T) *10];if (PB = = NULL) {cout<< "Allocate failed!! \ n "; exit (-1);} int *QB = new (pb) int (ten);d elete []pb;//Multiple delete or free a null pointer is not dangerous because delete first checks the case, the pointer is null, returns directly, But a non-NULL pointer can only be delete or free once int *pa = Null;free (P), free (p);d elete (p);d elete (p);}

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

The thinking and exploration of malloc free and New,delete--1

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.