C + + new usage

Source: Internet
Author: User

C + + new usage(2010-07-22 18:40:19) reproduced
Tags: gossip Category: C and MFC
There is a term called New in the C + + programming language. However, the concept is rather vague, and some people interpret it as the new function, but some people will interpret it as the new operator. So what does it really mean? Here we will help you analyze it by introducing the new usage of C + +.

One of the C + + new usages of the new operator

The most common is new as an operator, such as:

String *str = new String ("Test new");

As an operator, new and sizeof are built in C + +, and you can't make any changes to it, except to use it.

New allocates a chunk of memory on the heap and automatically calls the class's constructor.

The second new function of C + + new usage

The second is the new function, in fact, the new operator internally allocates memory using the new function, the prototype is:

void *operator New (size_t size);

The new function returns a void pointer, a piece of uninitialized memory. As you can see, this is similar to the C-language malloc behavior, you could reload the new function and add additional arguments, but you must ensure that the first parameter must be the size_t type, which indicates the size of the allocated memory block, C + + allows you to do so, and of course it is not necessary in general. If you overload the new function, the new operator is called when you use the new function.

If you use the new function, the code relative to the statement string *str = new String ("test new") might look like this:

1.string *str = (string*) operator new (sizeof (string));
2.str.string ("test new");
3.//Of course this call is illegal, but the compiler does not have this restriction

This is not complete, there is a third kind of new existence.

C + + new Usage III placement new

The third, placement new, is also a use of new as a function that allows you to allocate an object on an existing memory, while the memory data is not overwritten or is actively rewritten by you, placement new is also called by the new operator, and the calling format is:

New (buffer) type (size_t size);

First look at the following code:

4.char str[22];
5.int data = 123;
6.int *pa = new (&data) int;
7.int *PB = new (str) int (9);

The result *pa = 123 (the original data is not overwritten), and *PB = 9 (overwriting the original data), you can see that placement new does not allocate any additional memory, or it can use the memory allocated on the stack, not limited to the heap.

In order to use placement new you must include or

In fact, placement new is the same as the second, except that more arguments are overloaded with the function new, and the syntax is:

void *operator New (size_t, void* buffer);

It might look something like this:

void *operator New (size_t, void* buffer) {return buffer;}

And new corresponds to the delete, need to reclaim memory Ah, or leak, this next time write it, recall today's content first.

Summarize

1. Function new
void *operator New (size_t size); Allocate a piece of memory on the heap, and placement new (void *operator new (size_t, void* buffer)); Creating an object on an already existing memory, if you already have a piece of memory, placement new will be very useful, in fact, it is widely used in STL.
2. Operator new
The most commonly used new, there is nothing to say.
3. The function new does not automatically call the class's constructor because it knows nothing about the allocated memory type, and operator new automatically calls the class's constructor.
4. Function new allows overloading, while operator new cannot be overloaded.
5. The corresponding delete is immediately followed.

The above is a detailed introduction to C + + new usage.

C + + new usage

Related Article

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.