Pricing

Understanding the Role of New and Delete Functions in C++

C++ is one of the most commonly used languages in corporate development, and the New and Delete functions are both loved and hated by developers. Bugs and coredumps caused by New and Delete have made countless programmers work overtime.

Issues Encountered

In C++, you may often use new and delete to dynamically allocate and release memory, but have you ever thought about the following questions?

● Are new and delete functions?

● What are new [] and delete []? When should they be used?

● Are you familiar with operator new and operator delete?

● Why can arrays created with new [] sometimes be released with delete and sometimes not?

Discovered Code

Sometimes you might see code written like this:

Is there a problem with this? Will it cause memory leaks when running on a server for a long time?

I wrote a test program to see what was going on.

Program running results:

Notice the extra 4 bytes here.

Summary

This issue directly leads us to the need to preserve the dimension of an array when using new[] to allocate memory for an object array. The approach in C++ is to allocate an extra 4 bytes of memory when creating the array, specifically to store the array's dimension.

Delete [] can retrieve this stored value, allowing us to determine how often the destructor needs to be called.

Memory Allocation and Deallocation for C ++ Objects

Someone has raised two questions:

● How does the delete [] skip those 4 bytes?

● How does free know its length?

 

Let's explore these questions with some examples:

Delete Object:

Part of the assembly code is as follows:

Delete complex data type, first calls the destructor and then calls operator delete.

 

Memory Allocation in C++:

Let's examine how the M$ compiler constructs and releases memory.

Summary:

Memory allocation generally involves two approaches:

1. Non-intrusive: The memory allocator initially allocates memory (often in conjunction with the stack) for recording user-level allocation records (address and size). When the user releases memory, it looks up this table, which not only helps determine the size of the released memory but also validates the pointer's legality.

2. Intrusive: In this method, if a user requests 1 byte of memory, the allocator may allocate 5 bytes (for 32-bit systems), with the first 4 bytes used to store the requested size. When releasing memory, it first offsets by 4 bytes to find the allocated size and then performs the deallocation.

Both methods have their pros and cons. The first is secure but slower, while the second is faster but demands greater precision in pointer control from the programmer. Any slight oversight, like going out of bounds, can lead to the corruption of memory information.

The majority of allocators adopt the first method for implementation while operating system-level allocators use virtual techniques, potentially requiring the tracking of more information.

Analysis of Custom Array Types

So, how does the allocation and deallocation of arrays automatically increase and decrease by 4 bytes?

If you don't want to delve into all the details, let's focus on some key points, specifically the assembly code for creating new objects.

The general process of the assembly code is as follows:

1. Call the operator new to allocate heap space.

2. Call the constructor proxy function to construct the heap object. When calling the constructor proxy function, it passes five parameters through the stack, which are: a) the first heap object address, b) the heap object size, c) the number of heap objects, d) the constructor function address, and e) the destructor function address.

3. Return the address of the first heap object, not the address of the allocated heap space.

Let's take a look at the code for the destructor:

We can see that the destructor function automatically offsets 4 bytes.

The above assembly code may not explicitly show the 4-byte offset, so let's take a look at what the vector deleting destructor function does.

We can see that the function calls "operator delete[]" (0341244h), which subtracts 4 from the object address and then frees the memory. Now we know why the destructor function offsets 4 bytes.

About WeTest

WeTest Quality Open Platform is the official one-stop testing service platform for game developers. We are a dedicated team of experts with more than ten years of experience in quality management. We are committed to the highest quality standards of game development and product quality and tested over 1,000 games.

WeTest integrates cutting-edge tools such as automated testing, compatibility testing, functionality testing, remote device, performance testing, and security testing, covering all testing stages of games throughout their entire life cycle.

Latest Posts
1A review of the PerfDog evolution: Discussing mobile software QA with the founding developer of PerfDog A conversation with Awen, the founding developer of PerfDog, to discuss how to ensure the quality of mobile software.
2Enhancing Game Quality with Tencent's automated testing platform UDT, a case study of mobile RPG game project We are thrilled to present a real-world case study that illustrates how our UDT platform and private cloud for remote devices empowered an RPG action game with efficient and high-standard automated testing. This endeavor led to a substantial uplift in both testing quality and productivity.
3How can Mini Program Reinforcement in 5 levels improve the security of a Chinese bank mini program? Let's see how Level-5 expert mini-reinforcement service significantly improves the bank mini program's code security and protect sensitive personal information from attackers.
4How UDT Helps Tencent Achieve Remote Device Management and Automated Testing Efficiency Let's see how UDT helps multiple teams within Tencent achieve agile and efficient collaboration and realize efficient sharing of local devices.
5WeTest showed PC & Console Game QA services and PerfDog at Gamescom 2024 Exhibited at Gamescom 2024 with Industry-leading PC & Console Game QA Solution and PerfDog