C ++ new usage experience

Source: Internet
Author: User

In C ++ programming language, there is a term called new. However, this concept is vague. Some people regard it as a new function, but some may regard it as a new operator. So what is its true meaning? Here we will introduce the usage of C ++ new to help you analyze it together.

  • Interpretation of c ++ code examples for reading and writing text files
  • Summary of C ++'s experience in implementing RTTI
  • C ++ keyword popularity ranking
  • Detailed description of C ++ attributes
  • C ++ exception throwing skills

One of the usage of C ++ new, the new operator

The most common operator is the new operator, for example:

String * str = new string ("test new ");

As an operator, like sizeof, new is built-in C ++. You cannot make any changes to it except using it.

New allocates a block of memory on the stack and automatically calls the class constructor.

C ++ new usage 2 new Function

The second type is the new function. In fact, the memory allocated by the new operator is the new function. The prototype is:

Void * operator new (size_t size );

The new function returns a void pointer, an uninitialized memory. As you can see, this is similar to the malloc action in C. You can reload the new function and add additional parameters, but you must ensure that the first parameter must be of the size_t type, it specifies the size of the allocated memory block. C ++ allows you to do so. Of course, this is not necessary in general. If the new function is reloaded, the new function is called when the new operator is used.

If you use the new function, the code relative to the statement string * str = new string ("test new") is roughly as follows:

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

This is not complete yet, and there is a third new.

C ++ new usage 3 placement new

Third, placement new, which is also a usage of new as a function. It allows you to allocate an object on an existing memory, the data in the memory will not be overwritten or overwritten by you. placement new is also called by the new operator. The call format is:

New (buffer) type (size_t size );

Let's take a look at the following code:

 
 
  1. char str[22];  
  2. int data = 123;  
  3. int *pa = new (&data) int;  
  4. int *pb = new (str) int(9); 

Result: * pa = 123 does not overwrite the original data), and * pb = 9 overwrites the original data.) You can see that placement new does not allocate new memory, you can also use the memory allocated on the stack, not limited to the heap.

To use placement new, you must include <new> or <new. h>

In fact, the placement new is the same as the second one, but the parameter is added, which is a heavy load of the function new. The syntax format is:

Void * operator new (size_t, void * buffer );

It may look like this:

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

Delete corresponds to new, and memory needs to be recycled. Otherwise, it will be leaked. Write it again next time and recall the contents of today.

Summary

1. function new

Void * operator new (size_t size); allocate a memory on the heap, and placement newvoid * operator new (size_t, void * buffer); create an object on an 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

There is nothing to say about the most commonly used new.

3. The new function does not automatically call the class constructor because it does not know the allocated memory type. The new operator automatically calls the class constructor.

4. The new function allows overloading, while the new operator cannot.

5. The corresponding delete is followed.

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

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.