In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
Today, I would like to talk to you about the use of C++ new, which may not be well understood by many people. in order to make you understand better, the editor has summarized the following for you. I hope you can get something from this article.
There is a term called new in the C++ programming language. However, this concept is rather vague, some people understand it as a new function, but others will understand it as the new operator. So what does it really mean?
One of the uses of C++ new, the new operator
The most commonly used is new as an operator, such as:
String * str = new string ("test new")
As an operator, new, like sizeof, is built into C++, and you can't make any changes to it, except to use it.
New allocates a piece of memory on the heap and automatically calls the class's constructor.
The second part of C++ new usage: new function
The second is the new function. In fact, the internal memory allocation of the new operator uses the new function. The prototype is:
Void * operator new (size_t size)
The new function returns a void pointer, an uninitialized piece of memory. As you can see, this is similar to the malloc behavior of C language, you can overload the new function and add extra parameters, but you must make sure that the * parameters must be of type size_t, which indicates the size of the allocated memory block, and C++ allows you to do this, which is not necessary in general. If the new function is overloaded, the new function you overloaded will be called when you use the new operator.
If you use the new function, the code relative to the statement string * str = new string ("test new") looks like this:
String* str = (string*) operator new (sizeof (string)); str.string ("test new"); / / of course this call is illegal, but the compiler does not have this restriction
This is not over, there is a third kind of new.
The third usage of C++ new placement new
Third, placement new, which is also a use of new as a function, allows you to allocate an object on an existing piece of memory, and the data on the memory will not be overwritten or actively overwritten by you. Placement new is also called by the new operator in the following format:
New (buffer) type (size_t size)
Take a look at the following code first:
Char str [22]; int data = 123; int * pa = new (& data) int; int * pb = new (str) int (9)
As a result, * pa = 123 (not overwriting the original data) and * pb = 9 (overwriting the original data), you can see that placement new does not allocate new memory, and can also 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 one, except that it has more parameters, which is the overload of the function new. The syntax format is:
Void* operator new (size_t, void* buffer)
It might look like this:
Void* operator new (size_t, void* buffer) {return buffer;}
Corresponding to new is delete. Memory needs to be reclaimed, otherwise it will leak. Write this next time and recall today's content first.
Summary
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)); create objects on an existing piece of memory, placement new can be very useful if you already have a piece of memory, in fact, it is widely used in STL.
two。 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 type of memory allocated; the operator new automatically calls the class's constructor.
4. The function new allows overloading, while the operator new cannot be overloaded.
5. This is followed by the corresponding delete.
After reading the above, do you have any further understanding of the usage of C++ new? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.