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
1Steam Deck Compatible Games | Overview Find out about the expanding assemblage of steam deck compatible games that work perfectly on the interactive Steam Deck and provide portable gaming experiences in a variety of genres.
2Mastering PlayStation Portal Games Compatibility | Detailed Roadmap Are playstation portal compatible games? Find out the PlayStation Portal game compatibility which is important for the developers and the testers.
3Are Playstation Games Backwards Compatible | Detailed Explanation Are playstation games backwards compatible? Backward compatibility for PlayStation games is an essential feature and game testers must ensure this by following rigorous compatibility testing steps.
4Are Nintendo Switch Games Compatible With Switch Lite?  Are nintendo switch games compatible with switch lite? Find out the Nintendo Switch game compatibility with the Switch Lite which is important for the developers and the testers.
5This is QA on Easy Mode, Why Should You Try WeTest Automation Testing? With WeTest’s unique automated testing solutions, you can streamline your testing processes, ensure high-quality deliverables, and stay ahead in a competitive market.