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.


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.


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
1How Can I See FPS in Games | In-depth Review How can i see fps in games? FPS in games can be viewed by enabling the option in the game itself or by using third-party software.
2Let's Connect at GQF 2024: Meet Us in Person! WeTest will be participating as Silver Partner in GQF 2024. Come and visit!
3What is Quality Management for Games? Detailed Overview What is quality management in games? It is a systematic method of the attainment of pre-determined quality for games that enhances their quality through processes and methods.
4How to Write Bug Reports? In-depth Review How to write a bug report: Learn how to make effective bug reports aimed at helping developers easily understand them, pinpoint the bugs and start working on their elimination.
5How To Make Test Cases in Software Testing? In-depth Review How to make test cases in software testing: Using this guide game testers can learn about how to develop proper test cases for software testing of the games to achieve good quality games.