Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use new and delete in C++

2025-04-05 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

Editor to share with you how to use new and delete in C++, I believe most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, now let's go to understand it!

New expression

New a type that creates a memory of that type, then calls the constructor, and finally returns a pointer to that memory

Note: this operation is atomic.

The implementation in vc6 is as follows

Void * operator new (size_t size, const std::nothrow_t &) _ THROW0 () {void * p while ((p = malloc (size)) = 0) {/ / if the call to malloc fails, it will be called _ callnewh / / _ callnewh means callnew handler. To put it simply, the user sets a callback function / / use _ set_new_handler to set it. Usually, the user controls the release of some unused memory _ TRY_BEGIT if (_ callnewh (size) = = 0) break _ CATCH (std::bad_alloc) return (0); _ CATCH_END} return (p);} delete expression

Delete a pointer that first calls the destructor and then frees memory

The implementation in vc6 is as follows

Void * operator delete (void * p) _ THROW0 () {free (p);} new [] and new ()

New [] is the allocation pointer array, new () is the allocation of direct initialization, these two are easy to get confused, the key is to compile can be passed, be sure to pay attention. For example:

Int* p = new [3]; / is an array of pointers consisting of three int* pointers int* Q = new (3); / / allocates an int heap memory and initializes it to 3new [] and delete []

Complex * pca = new Complex [3]

Call the constructor of Complex three times to allocate three Complex objects

Delete [] pca

Free memory.

What if the delete [] here is only written as delete? A lot of people will say: memory leak.

In fact, the correct answer is not sure. It depends on whether there is heap memory inside the Complex class.

What is the memory like after new []? Look at the picture below.

The key is to look at the cookie part of the figure, which stores some memory-related data, the most important of which is the size of allocated memory in cookie.

Let's take a look at the following code

String * psa = new string [3]; delete psa

After the code is executed, the memory allocation is as follows

Because dynamic heap memory is used inside the string class to hold strings, the cookie of the memory allocated by new [] records only the information of the string class, while the dynamic heap memory information within the class is managed by each instance and is not in the cookie of new [].

As mentioned earlier, the process of freeing memory in delete is to call the destructor before freeing memory. In this case, if you use delete [] to free memory, each instance's destructor is called in turn, and each destructor frees its own internal heap memory, and then frees new's memory block. But if you use delete to free memory, the first instance will only call the destructor once, and the other two instances will not, and then the memory allocated by new will be released according to the amount of memory recorded in cookie, and the heap memory in the other two instances will be leaked.

In other words, for the example of string in the figure above, if you use delete to release memory directly, the memory shown in the white area to the right of the str2 and str3 arrows is leaked, while the green area to the right of the pas arrow can be released correctly (whether the called str1 or str3 depends on the specific implementation of the compiler, just understand what it means).

However, this does not mean that you can use delete to release new without any scruples in the case of no heap memory inside the class. This is a problem with the coding specification. Using delete is not necessarily wrong, but using delete [] is definitely correct.

Memory distribution of new

The following figure shows the memory layout of new in vc6

What we get is the pointer to the 0x00441c30 part of the figure, but the memory actually manages all the large chunks of memory in the diagram, of which the orange part is only available in debug mode. Since memory management needs to be a multiple of 16, if it is less than a multiple of 16, add some data to a multiple of 16. The blue pad part of the figure is the added useless data. The 61h part of the figure is cookie, and the upper and lower parts are the upper cookie and the lower cookie, respectively. Since this example uses an example of the int type, and int is not deconstructed, you can release the whole block of memory by using delete directly. This is written here in order to deepen the reader's understanding. [] should be added when the actual coding is made. Compare this with the following figure.

The type used in this diagram is a class. When you allocate memory with new [], there is an extra piece of memory between the returned pointer and the debugging information to record the number of instances, which is 3 in the figure. In this case, if delete [] is used to free memory, it will be correctly indexed to the first address of the instance for release operation. If delete is used to free memory, the indexed memory is the location of integer data that records the number of instances. If you start to destruct the memory structure of this class by looking for the memory structure of this class, there will certainly be problems, and the whole memory structure will be messed up.

There is one thing to note here. The delete and delete [] sections here seem to contradict the summary of new [] and delete []. What did the teacher say? since I was watching pirated online lessons, I couldn't ask the teacher for advice. I don't know exactly what the situation is. Guess is because the specific implementation of different compilers, the position of 3 is different, some in the front, some in the back, the key is to see the specific implementation, in the front of the situation is contradictory, in the back is fine, the key is to understand the spirit.

Placement new

Placement new allows us to build objects in an already allocated memory

There is no so-called placement delete, because placement does not allocate memory at all, it just uses a memory that has already been allocated, so there is no need for a corresponding release operation, the specific usage is as follows

# include / / allocate memory char * buf = new char [sizeof (Complex) * 3]; / / construct ComplexComplex * pc = new (buf) Complex (1, 2) on the allocated memory; / / Note the pointers to be released here / / feel that if you release the pc directly, it should be right / / there is no environment on the hand and cannot be tested. You will have time to test the delete [] buf;new failure processing later.

When using malloc to allocate memory in pure C, you need to judge the returned pointer. If a null pointer is returned, it means that the memory allocation has failed.

When it comes to c, using new to allocate memory, it is impossible to determine whether it fails by judging a null pointer. Because in c, if new fails, an exception will be thrown, and the code cannot go to the statement that determines the null pointer. There are several correct ways to deal with new failure.

Catch exception try {int* p = new int [SIZE]; / / other code} catch (const bad_alloc& e) {return-1;}

It is said that the ancient C++ compiler new failure will not throw an exception, but return a null pointer like malloc, because at that time C++ did not have an exception mechanism, spread among the community, and did not bother to verify, just understand the following. By the way, complaining that the abnormality of c is shit, which is an insult to shit. Shit can also be used as fertilizer for farming. C abnormality is of no use except to make trouble.

Disable new exception int* p = new (std::nothrow) int; / / so that if new fails, it does not throw an exception, but returns a null pointer new-handler

As mentioned at the beginning of the article when introducing the source code of new, the callback function of new-handler will be called when new is implemented, and the callback function can be set before new. As this method is too troublesome to study, the specific usage of readers to find the relevant information.

Heavy load

When overloading, generally do not overload the global:: operator new, because the global influence is too great, generally only overload the Foo::operator new of the class itself.

Overloading is commonly used in memory pools, so cookie can be reduced.

Overload global:: operator newvoid * myAlloc (size_t size) {return malloc (size);} void myFree (void * p) {free (p);} / / the following code implementation is not important, it depends on the overloading of the interface / / they cannot be declared in a namespace inline void * operator new (size_t size) {cout

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report