๐น What's an Array?
An array is a data structure that holds multiple values of the same data type in contiguous memory locations.
Letโs break this down:
- Whatโs a Data Structure? A data structure is a specific way of storing and accessing data in a computer so it can be used efficiently. Common operations: Read, Write, Update, Delete.
- We choose different data structures based on our needs. Some examples:
- Arrays (specifically 2D arrays or matrices) are used to represent pixels in an image. Since array elements are stored in contiguous memory, accessing them is very fast โ something that's critical in graphics rendering.
- Trees are a natural choice for implementing file systems. Folders nested within folders can be best modeled as trees, with the root folder at the top.
๐น Why Arrays?
Arrays are optimized for locality of reference โ a key concept in making memory access fast.
Locality of Reference:
This refers to the tendency of programs to access:
- Recently used memory locations โ called Temporal Locality
- Nearby memory locations โ called Spatial Locality
These patterns are utilized by:
- The CPU: When the CPU fetches data from memory, it also fetches nearby memory locations, anticipating future access.
-
Programmers: Take an image processing task where pixels are stored in a matrix (2D array). In most programming languages, matrices are stored in row-major order, meaning all columns of row 1 are stored first, then row 2, and so on.
So consider this code:
for col in range(N): for row in range(N): sum += matrix[row][col]
This accesses data column-first, skipping memory in the way itโs laid out, leading to cache misses. A better approach would be to access data row-first.
๐ Why arrays are fast:
- Array elements are stored contiguously (spatial locality).
- When you access
array[0]
, the CPU preloadsarray[1]
,array[2]
, etc., into the CPU cache. - This makes index-based access blazing fast. Hence, arrays are ideal when:
- You need to store large amounts of data of the same type
- You need to perform common operations across all elements, like scaling pixels in an image.
๐น Here's a mind map of everything I just learned โ whatโs yours?
Next Up:
Dynamic arrays โ what happens when we want to grow or shrink arrays on the fly?
Hope you enjoyed reading!
Let me know if something wasnโt clear or if youโve got a better mental model!
Top comments (0)
Some comments may only be visible to logged-in visitors. Sign in to view all comments.