C++: Why new an array of class with destructor will need another 8 bytes to store the number of objects?
Abstract: Explains why C++ new[] allocates extra bytes for array size metadata and why direct access is undefined behavior.
2026-10-23
Created: 2026-10-23 by
UserComp.com Editors
When you allocate an array of objects with non-trivial destructors using new[], the runtime often stores metadata, such as the element count, to ensure all destructors are called correctly during deallocation. This overhead is not a fixed 8-byte standard but an implementation-specific detail determined by your compiler and platform. Understanding this mechanism helps explain why raw pointer arithmetic on new[] results often differs from the size of the object array itself.
The Mechanism Behind Array Allocation
In C++, when you use new T[n], the memory manager must know how many objects were created so that delete[] can invoke the destructor for each instance in reverse order. For types with trivial destructors, this information might be optimized away or stored differently. However, for classes with non-trivial destructors, the allocator typically reserves extra space immediately preceding the returned pointer.
This extra space acts as a header. It usually contains the number of elements allocated. When you call delete[] ptr, the runtime reads this hidden count from ptr - offset to determine how many times to call the destructor. The exact size of this header varies significantly between compilers. On some 32-bit systems, it might be 4 bytes; on 64-bit systems, it is often 8 bytes. Some implementations may even store additional flags or alignment padding, making the overhead larger than just the count.
It is crucial to note that this behavior is an implementation detail defined by the C++ standard library vendor, not a language mandate. The C++ standard requires that delete[] works correctly, but it does not specify the internal layout of the allocation header. Therefore, assuming a specific byte size like 8 bytes is incorrect because it will fail on other platforms or compiler versions where the header size differs.
Why You Cannot Access This Metadata Directly
You might wonder if you can access this count manually. The answer is no, and attempting to do so leads to undefined behavior. The C++ standard explicitly forbids performing pointer arithmetic that crosses the boundary of the allocated block in ways that assume knowledge of internal headers.
If you try to cast the pointer to an integer type and subtract an offset, you are relying on assumptions about the compiler's internal data structures. For example, on Visual Studio, the header might contain multiple fields, including the size and possibly other bookkeeping data. On GCC or Clang, the layout might differ entirely. Even if you discover the offset on your current machine, changing the optimization level, switching to a different version of the compiler, or moving to a different operating system can break your code completely.
Furthermore, the standard states that the pointer returned by new[] points to the first element of the array. Any attempt to read memory before this pointer is technically out of bounds unless you are using specific, well-defined techniques provided by the standard library, which do not expose this header directly.
Safe Alternatives for Managing Object Arrays
Because of these complexities and portability issues, modern C++ strongly discourages the use of raw new[] and delete[] for managing arrays of objects. Instead, you should use containers from the Standard Template Library (STL) that handle memory management automatically and safely.
The primary alternative is std::vector. When you create a std::vector, the vector class internally manages the dynamic array. It handles the allocation of extra space for metadata, the construction of elements, and the destruction of elements when they go out of scope or when the vector is resized. This ensures that exception safety is maintained properly, unlike raw arrays where an exception during construction could lead to memory leaks.
Another option is std::unique_ptr, which provides a smart pointer wrapper for arrays. While it still uses delete[] under the hood, it ensures that the memory is freed exactly once when the unique pointer goes out of scope. However, it does not provide the same convenient interface as std::vector for resizing or accessing elements by index without care.
For most use cases, std::vector is the recommended choice. It abstracts away the low-level details of memory management, including any hidden headers used by the allocator. This allows you to focus on the logic of your program rather than the intricacies of how the runtime tracks object counts.
Impact on Memory Management and Debugging
Understanding that new[] has overhead is important for debugging memory issues. Tools like Valgrind or AddressSanitizer track allocations based on the actual bytes requested from the system allocator. If you request n * sizeof(T) bytes, the runtime might request more from the OS to include the header. This can sometimes confuse developers who expect the allocated size to match the object size exactly.
Additionally, this overhead impacts memory efficiency. For very small objects allocated in large quantities, the header overhead can become significant relative to the object size. In performance-critical applications, this might lead to higher memory usage than expected. Using std::vector can sometimes mitigate this if the vector implementation optimizes capacity growth, but the fundamental overhead of tracking destructors remains for non-trivial types.
For types with trivial destructors, some allocators might avoid storing the count altogether, relying on other mechanisms or simply ignoring the need for per-element destruction. This further highlights why the "8 bytes" rule is not universal. Always test your memory usage assumptions on the target platform rather than assuming a generic behavior.
Common Misconceptions About Pointer Arithmetic
A common mistake among beginners is believing that they can calculate the number of elements by inspecting the memory before the pointer. This is unsafe and unreliable. As mentioned earlier, the layout is not standardized. Even within the same compiler, different builds (debug vs. release) might use different allocation strategies.
Debug builds often insert guard patterns or additional metadata to help detect buffer overflows and use-after-free errors. This means the header size in debug mode might be much larger than in release mode. Relying on this for production code is dangerous. The only reliable way to know the number of elements is to store it yourself or use a container that stores it for you.
Another misconception is that sizeof can help determine the allocation size. sizeof returns the size of the type at compile time. It cannot tell you how much memory was actually allocated at runtime, especially when headers are involved. You must rely on runtime tools or careful design to manage memory correctly.
Conclusion
The extra bytes used by new[] are necessary for correct cleanup of objects with destructors, but their size and location are implementation-defined. Avoid relying on specific byte counts like 8 bytes, as this leads to non-portable code. Use std::vector or std::unique_ptr to let the standard library handle these details safely and efficiently. This approach ensures your code works across different compilers and platforms while maintaining strong exception safety guarantees.
By adhering to these practices, you prevent memory leaks and undefined behavior associated with manual array management. The C++ ecosystem provides robust tools for dynamic memory management; leveraging them reduces complexity and improves reliability in your software projects.
Frequently Asked Questions
Is the overhead always 8 bytes?
No, the overhead is implementation-specific and depends on the compiler and platform. It can be 4 bytes, 8 bytes, or even larger depending on the allocator's needs for alignment and metadata.
Can I disable this overhead for my class?
You cannot directly disable it, but you can define a trivial destructor. If the destructor is trivial, the compiler might optimize away the need to store the count, though this behavior is not guaranteed across all implementations.
What happens if I use delete instead of delete[]?
Using delete on memory allocated with new[] results in undefined behavior. The runtime expects to call destructors for all elements, and using delete only destroys the first object, leading to memory leaks and potential crashes.
Does std::vector have similar overhead?
Yes, std::vector also manages dynamic memory and may incur similar overhead internally. However, it abstracts this away from you, ensuring that the correct amount of memory is freed and destructors are called reliably.
How can I check the actual allocated size?
You can use platform-specific functions like msize on Windows or malloc_usable_size on Linux to query the total size of a memory block, including any headers. These are non-standard extensions and should be used with caution.