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 realize memory Management in cUniverse +

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

The main purpose of this article is to show you "how to achieve memory management in cUniverse +". The content is easy to understand and clear. I hope it can help you solve your doubts. Let me lead you to study and learn this article "how to achieve memory management in cAccord +".

Memory area:

From this picture, we can see the distribution of programs in memory, different variables are stored in different areas, and the kernel space is used to store relevant information that users cannot read or write.

To note that the stack grows down, the stack grows up, and the stack grows relative to each other. today we are mainly talking about applying for space on the heap. We know that functions such as malloc, realloc and calloc are needed to apply for space on the heap in C language. about the similarities and differences of these functions, you can refer to my other blog.

In C++, we use the key words new and delete to complete the application and release of space. Syntax is also to define a pointer variable to accept the open address, int p=new int;c++ allows us to apply for the space initialization, as long as followed by (), () for the content to be initialized. You can also apply for contiguous memory space using int p=new int [];, which is delete [] p; when freed.

Note: when applying for custom types, new and malloc are similar, which means that the built-in types free and delete can be used together, and the space applied for with new will be released with free without any error, while for user-defined types new and delete, constructors and destructors will be called respectively, while malloc and free will not. Malloc requests a memory space of the same size as the object and will not constitute an object. So we should pay attention to matching when applying for and releasing space!

Let's talk about the principle of malloc here: we know that when malloc applies for space with a size of 10 bytes, the system will apply for space greater than 10 bytes for us.

This means that when we are in free, the system will know how many bytes of free, because the value of the size of our application space has been saved at the top of this structure-like structure.

Operator new and operator delete function

These two functions are called by new and delete at the bottom, that is, these two functions complete the development and release of memory for us.

Let's first look at the function operator new:

Void * _ CRTDECL operator new (size_t size) _ THROW1 (_ STD bad_alloc) {/ / try to allocate size bytes void * p; while ((p = malloc (size)) = = 0) if (_ callnewh (size) = = 0) {/ / report no memory / / if the memory application fails, a bad_alloc type exception static const std::bad_alloc nomem will be thrown here _ RAISE (nomem);} return (p);}

From this function, we can see that the underlying layer is also implemented through malloc. If the malloc application is successful, it will be returned directly. If the malloc application fails, you will try to implement the response measure of insufficient space. If you implement this measure, you will continue to apply, otherwise an exception will be thrown. Therefore, the new function generally does not fail to apply.

Let's take a look at the function operator delete:

Void operator delete (void * pUserData) {_ CrtMemBlockHeader * pHead; RTCCALLBACK (_ RTC_Free_hook, (pUserData, 0)); if (pUserData = = NULL) return; _ mlock (_ HEAP_LOCK); / * block other threads * / _ _ TRY / * get a pointer to memory block header * / pHead = pHdr (pUserData) / * verify block type * / _ ASSERTE (_ BLOCK_TYPE_IS_VALID (pHead- > nBlockUse)); _ free_dbg (pUserData, pHead- > nBlockUse); _ _ FINALLY _ munlock (_ HEAP_LOCK); / * release other threads * / _ _ END_TRY_FINALLY return;}

Through this, we know that the underlying layer is also implemented through free, namely ok.

Let's take a look at how it works for custom types new and delete:

The principle of new

Call the operator new function to apply for space

Execute the constructor on the applied space to complete the construction of the object

The principle of delete

Execute the destructor in space to clean up the resources in the object

Call the operator delete function to free up the object's space

The principle of new T [N]

Call the operator new [] function and actually call the operator new function in operator new [] to complete the application of N object spaces.

Please

Execute the N-time constructor on the applied space

Principle of delete []

Execute the N-th destructor on the released object space to clean up the resources in N objects.

Call operator delete [] to free up space, and actually call operator delete in operator delete [] to free up space

Finally, let's talk in detail about the difference between malloc and new:

1.new is an operator, malloc is a function

2.new can initialize the open space, but malloc cannot.

When using 3.malloc, you need to calculate the size of the opened memory (bytes) and pass it, while new only needs to keep up with the type.

The return value of 4.malloc is void, so to force type conversion, type after new is not required.

A failed 5.malloc application returns NULL, so to make a null decision, new does not need to catch an exception.

6.malloc and free do not call constructors and destructors.

The above is all the contents of the article "how to implement memory Management in chammer censor +". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

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