Understanding memory layout is understanding performance.
<aside> 🔗
Related Topics:
The foundation of understanding Go slices starts with how C handles arrays.
int a[4])Memory holds integers directly. Data is contiguous.
[ STACK ]
Variable 'a' (16 bytes):
[ 10 | 20 | 30 | 40 ]
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>
When you use array a in an expression, the compiler automatically treats it as a pointer to the first element (&a[0]).
Exceptions:
sizeof(a) → Returns size of entire array&a → Returns address of the array block*(a + i) works