> ## Documentation Index
> Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt
> Use this file to discover all available pages before exploring further.

# C++ Array Deallocation

The `delete[]` operator deallocates a block of memory previously allocated for an array of objects via the `new[]` operator. It ensures proper object lifecycle management by invoking the destructor for each element in the array before returning the raw memory to the free store (heap).

```cpp theme={"dark"}
delete[] pointer;
```

## Mechanics of Execution

When `delete[]` is evaluated, the C++ runtime performs two distinct operations sequentially:

1. **Destruction:** The runtime iterates through the array and invokes the destructor (`~T()`) for every initialized object. The destructors are strictly called in the reverse order of their construction (from the highest index down to index `0`).
2. **Deallocation:** After all destructors have completed, the runtime invokes the deallocation function—either the global `operator delete[](void*)` or a class-specific overload—to release the contiguous memory block back to the system.

## Array Size Tracking (The "Cookie" Idiom)

Because `delete[]` only receives a pointer to the first element, the runtime must determine the number of elements to destroy. Compilers typically resolve this by allocating extra memory (often called an "array cookie" or metadata block) immediately preceding the memory address returned by `new[]`. When `delete[]` is invoked, the runtime reads this hidden metadata to determine the exact number of destructors to execute.

## Technical Constraints and Undefined Behavior (UB)

* **Mismatched Operators:** Using the scalar `delete` operator on a pointer allocated with `new[]`, or using `delete[]` on a pointer allocated with scalar `new`, results in undefined behavior. This typically causes heap corruption or segmentation faults because scalar `delete` does not look for the array cookie and will fail to invoke the destructors for elements beyond the first.
* **Null Pointers:** Applying `delete[]` to a `nullptr` is a guaranteed no-operation. The runtime performs a null check before attempting destruction or deallocation.
* **Double Free:** Invoking `delete[]` more than once on the same memory address results in undefined behavior, usually triggering a heap corruption exception or program crash.
* **Polymorphic Arrays:** Invoking `delete[]` on an array of derived-class objects via a base-class pointer results in undefined behavior. Pointer arithmetic relies on the static type's `sizeof`, meaning the runtime will calculate incorrect memory offsets for the destructors if the base class and derived class differ in size.

<div
  style={{ 
display: "flex", 
justifyContent: "space-between", 
alignItems: "center", 
maxWidth: "754px", 
padding: "1rem 0",
marginBottom: "24px"
}}
>
  <span style={{ fontWeight: "bold", fontSize: "1.25rem", color: "var(--tw-prose-headings)", fontFamily: "Inter, ui-sans-serif, system-ui, sans-serif" }}>Tired of Poor C++ Skills? Fix That With Deep Grasping!</span>

  <a
    href="https://syntblaze.com"
    target="_blank"
    style={{ 
  marginLeft: "24px",
  textDecoration: "none", 
  backgroundColor: "#007AFF",
  color: "#ffffff", 
  padding: "6px 16px", 
  borderRadius: "16px",
  fontSize: "0.9rem",
  fontWeight: "600",
  textAlign: "center",
  transition: "background-color 0.2s ease"
}}
  >
    Learn More
  </a>
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/skill-tracking.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=b9b0305c93bb501c9e767b5c76c88835" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/skill-tracking.png" />

  <img src="https://mintcdn.com/syntblazellc/23tyuOzaWS88qFlc/images/nuggets.png?fit=max&auto=format&n=23tyuOzaWS88qFlc&q=85&s=c86c80197299762989e9b882419b2109" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/nuggets.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/bite-sized-exercises.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=a65f9a38c37ff28ab73ed783c53c60e3" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/bite-sized-exercises.png" />
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap", marginTop: "12px" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/mastery-chain.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=748a1763454713e679260fbb95f154a2" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/mastery-chain.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-previews.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=242f61448ff5dd6deaaab2dccc13b507" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-previews.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-explanations.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=cf0fc1c31f9cd0fc26716781be05fbc9" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-explanations.png" />
</div>
