Arrays are a fundamental data structure in computer science, used to
store and manipulate large collections of data. In this chapter, we learn
how arrays are represented in memory and how we can use this
knowledge to optimize our code.
Memory Representation
In order to understand how arrays are stored in memory, we first need
to understand how memory works. Memory is a large collection of bytes,
each with its own unique address. When we declare a variable, the
operating system assigns it a block of memory and stores its address in
a register.
For example, let's say we declare an integer variable x and assign it the
value 5:
int x = 5;
The operating system might assign x the memory address 0x1000, and
store the value 5 at that address.
Arrays
An array is a collection of variables of the same type, stored in
contiguous blocks of memory. This means that if we declare an array of
integers int arr[10], the operating system will assign it a block of
memory big enough to store 10 integers, and each integer will be
stored in a contiguous block of memory.
For example, let's say we declare an array of integers int
arr[10] and initialize it with the values {1, 2, 3, 4, 5, 6, 7, 8,
9, 10}. The operating system might assign arr the memory
address 0x2000, and store the values as follows:
| Address | Value | | --- | --- | | 0x2000 | 1 | | 0x2001 | 2 | | 0x2002 | 3 | |
0x2003 | 4 | | 0x2004 | 5 | | 0x2005 | 6 | | 0x2006 | 7 | | 0x2007 | 8 | |
0x2008 | 9 | | 0x2009 | 10 |
, We can access the elements of an array using their index, which is an
integer starting at 0 and increasing by 1 for each element. For
example, arr[0] refers to the first element of the array, arr[1] refers
to the second element, and so on.
Accessing Arrays
Accessing elements of an array is a common operation in programming,
but it can be slow if we're not careful. This is because accessing memory
involves reading and writing to physical locations on the computer,
which can take time.
To optimize array access, we can use a technique called blocking.
Blocking involves dividing an array into smaller blocks, and accessing
each block sequentially. This reduces the number of times we have to
access memory, which can improve performance.
For example, let's say we have an array arr[100] and we want to
access every 10th element. Instead of
accessing arr[0], arr[10], arr[20], and so on, we can access the
array in blocks of 10 elements. We can start by accessing the first 10
elements, then move to the next 10 elements, and so on. This reduces
the number of memory accesses by a factor of 10, which can
significantly improve performance.
As programmers, it's important to understand how arrays are
represented in memory and how we can optimize our code to access
them efficiently. By using techniques like blocking, we can write faster
and more efficient code that takes advantage of the underlying
hardware.
I hope this summary has been helpful in introducing the concept of
arrays and memory representation. Happy coding!