Understanding memory layout is understanding performance.

<aside> 🔗

Related Topics:


Core Concept: Value Arrays vs Pointer Arrays

The foundation of understanding Go slices starts with how C handles arrays.

Value Array (int a[4])

Memory holds integers directly. Data is contiguous.

[ STACK ]
Variable 'a' (16 bytes):
[ 10 | 20 | 30 | 40 ]

Pointer Array (int *p[4])

Memory holds pointers that refer to integers stored elsewhere.

[ STACK ]                     [ HEAP ]
Variable 'p':
[ ptr ] ─────────────────────► [ 10 ]
[ ptr ] ─────────────────────► [ 20 ]
[ ptr ] ─────────────────────► [ 30 ]
[ ptr ] ─────────────────────► [ 40 ]

<aside> 💡

Why this matters: Python/Java Lists are pointer arrays. The CPU must jump around memory twice to get the actual value — causing cache misses and slow performance.

</aside>


C Array Behavior: Decay & Dereference

What is "decay"?

When you use array a in an expression, the compiler automatically treats it as a pointer to the first element (&a[0]).

Exceptions:

Why *(a + i) works