Three usages of new in C + + detailed resolution of _c language

Source: Internet
Author: User
Tags arrays

I. INTRODUCTION
There are three ways to use NEW: plain New,nothrow new and placement new.

(1) Plain new, as the name suggests, is the ordinary new, is our usual use of new. This is defined in C + +:
void* operator New (std::size_t) throw (Std::bad_alloc);
void operator delete (void *) throw ();

Tip:Plain New throws an exception std::bad_alloc instead of returning null in the event of an allocation failure, so it is futile to judge whether the return value is null or not.

(2) nothrow new is the form of operator new that does not throw an exception. Nothrow new returns NULL when it fails. the definition is as follows:
void * operator new (Std::size_t,const std::nothrow_t&) throw ();
void operator Delete (void*) throw ();

(3) Placement new means "placement", which allows the object or array of objects to be reconstructed on a piece of memory that has already been allocated successfully. placement new does not have to worry about memory allocation failure because it does not allocate memory at all, the only thing it does is call the object's constructor. The definition is as follows:
void* operator new (size_t,void*);
void operator Delete (void*,void*);

Tip 1:The main purpose of palcement new is to reuse a large dynamically allocated memory to construct different types of objects or their arrays.

Tip 2:placement new constructs the object or its array, to display the call their destructor to destroy, do not use delete.

char* p = new (nothrow) char[100];
Long *q1 = new (P) long (100);
int *q2 = new (P) int[100/sizeof (int)];

Two. Examples

1.plain New/delete. Ordinary New
The definition is as follows:
void *operator New (std::size_t) throw (Std::bad_alloc);
void operator Delete (void*) throw ();

Note: standard C + + plain throws a standard exception std::bad_alloc instead of returning null when new fails, so it is futile to check that the return value is null to determine whether the assignment succeeded.

Test program:

Copy Code code as follows:

#include "stdafx.h"
#include <iostream>
using namespace Std;

Char *getmemory (unsigned long size)
{
Char *p=new char[size];//allocation failed, not return null
return p;
}

int main ()
{
Try
{
Char *p=getmemory (10E11);//allocation failure throws exception Std::bad_alloc
//...........
if (!p)//In vain
cout<< "Failure" <<endl;
delete [] p;

}
catch (const Std::bad_alloc &EX)
{
Cout<<ex.what () <<endl;
}

return 0;
}


2.nothrow New/delete does not throw an exception in the form of operator new, which returns NULL when new fails.
The definition is as follows:
Copy Code code as follows:

void *operator New (Std::size_t,const std::nothrow_t&) throw ();
void operator Delete (void*) throw ();
struct nothrow_t{}; Const nothrow_t Nothrow;//nothrow As the symbolic dummy of new

Test program:
Copy Code code as follows:

#include "stdafx.h"
#include <iostream>
#include <new>
using namespace Std;

Char *getmemory (unsigned long size)
{
Char *p=new (nothrow) char[size];//allocation failed, return null
if (null==p)
cout<< "Alloc failure!" <<endl;
return p;
}

int main ()
{
Try
{
Char *p=getmemory (10E11);
//...........
if (p==null)
cout<< "Failure" <<endl;
delete [] p;

}
catch (const Std::bad_alloc &EX)
{
Cout<<ex.what () <<endl;
}

return 0;
}


3.placement New/delete main uses are:A large, dynamically allocated memory is used repeatedly to construct different types of objects or their arrays. For example, you can request an array of characters that is large enough, and then construct different types of objects or arrays on top of it when needed. Placement new does not have to worry about memory allocation failure because it does not allocate memory at all, it simply invokes the object's constructor.

Test program:

Copy Code code as follows:

#include "stdafx.h"
#include <iostream>
#include <new>
using namespace Std;

Class ADT
{
int i;
Int J;
Public
ADT ()
{
}
~adt ()
{
}
};

int main ()
{
Char *p=new (nothrow) char[sizeof (ADT) +2];
if (p==null)
cout<< "Failure" <<endl;

ADT *q=new (p) ADT; Placement NEW: Don't worry about failure
Delete q;//Error! Cannot call delete q here;
Q->adt::~adt ()//Display Call destructor
delete []p;
return 0;
}


Note:objects or arrays that are constructed with placement new are explicitly called to destroy (destructors do not release the object's memory), never use Delete. This is because placement The new constructed object or array size does not necessarily equal the size of the original allocated memory, and using delete causes a memory leak or a run-time error when memory is released later.

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.