Home >Backend Development >C++ >Heap vs. Stack Allocation: Where Do Arrays and Value Types Live in Memory?

Heap vs. Stack Allocation: Where Do Arrays and Value Types Live in Memory?

Mary-Kate Olsen
Mary-Kate OlsenOriginal
2025-01-02 17:53:44649browse

Heap vs. Stack Allocation: Where Do Arrays and Value Types Live in Memory?

Heap and Stack Allocation: Breaking Down Arrays and Value Types

In the presented code snippet:

int[] myIntegers;
myIntegers = new int[100];

the array myIntegers is generated on the heap. Contrary to popular belief, the actual integer values stored within the array are not boxed.

Clarifying Stack and Heap Allocation

The common misconception that value types are allocated on the stack and reference types on the heap is not entirely accurate. All local variables and parameters, regardless of type, are allocated on the stack. The key distinction lies in what is stored in these variables:

  • Value types: The actual value is stored directly in the variable.
  • Reference types: A reference to the value stored on the heap is stored in the variable.

Memory Allocation for Value and Reference Types

Both value and reference types occupy a specific amount of memory, typically 16 bytes for the example types provided. Within this memory:

  • For value types, all fields store their respective values directly.
  • For reference types, reference-type fields store references to values located on the heap, while value-type fields store their actual values.

Example: Stack and Heap Allocation

Consider the following local variables:

RefType refType;
ValType valType;
int[] intArray;

Initially, only their references reside on the stack, occupying 20 bytes:

 0 ┌───────────────────┐
   │     refType       │
 4 ├───────────────────┤
   │     valType       │
   │                   │
   │                   │
   │                   │
20 ├───────────────────┤
   │     intArray      │
24 └───────────────────┘

Upon assigning values to these variables:

// Assign values to refType
// ...
// Assign values to valType
// ...
intArray = new int[4];
intArray[0] = 300;
// ...
intArray[3] = 303;

The stack representation would resemble:

 0 ┌───────────────────┐
   │    0x4A963B68     │ -- heap address of `refType`
 4 ├───────────────────┤
   │       200         │ -- value of `valType.I`
   │    0x4A984C10     │ -- heap address of `valType.S`
   │    0x44556677     │ -- low 32-bits of `valType.L`
   │    0x00112233     │ -- high 32-bits of `valType.L`
20 ├───────────────────┤
   │    0x4AA4C288     │ -- heap address of `intArray`
24 └───────────────────┘

Both the array and the value and reference types occupy specific sections of the heap, with memory layouts as described before.

In summary, the array myIntegers is allocated on the heap, while the individual integer values stored within it are not boxed. They are simply stored directly within the allocated memory on the heap, without the need for wrapping them in additional objects.

The above is the detailed content of Heap vs. Stack Allocation: Where Do Arrays and Value Types Live in Memory?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn