Customer Cases
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
1LLM-Driven Intelligent Testing: Core Concepts, RAG Integration, and Advanced Scenarios Explore the deep integration of Large Language Models (LLMs) in intelligent testing. Learn how RAG and AI Agents revolutionize requirement analysis, test case generation, root cause analysis, and strategy optimization.
2Intelligent Testing System: Enterprise Implementation Path & Trends 2026 A complete guide to intelligent testing system, covering 5-layer architecture, 4 core modules, enterprise implementation path, team building & real cases for quality, efficiency & cost reduction.
3Shift Left Testing & Shift Right Testing: Building a Full-Lifecycle Quality Assurance System Discover the core principles, implementation practices, and enterprise case studies of Shift Left Testing and Shift Right Testing. Learn how to build a full-lifecycle intelligent quality assurance system to reduce defects and ensure production stability.
4Low-Code/No-Code Testing Platform Practices: Mabl, Testim, Applitools Guide Explore low-code/no-code testing practices with Mabl, Testim, Applitools. Learn core concepts, practical operations, enterprise implementation strategies, and how to improve testing efficiency significantly.
5Cloud-Native Testing: Strategies for Containerized & Distributed Environments Cloud-native testing guide for testers: Learn container (Docker) & microservice testing strategies, tools (Sentinel, SkyWalking), real cases, and pitfalls to ensure distributed system stability in 2026.