Arrays Versus Pointers at the Hardware Level

In C, arrays and pointers often look interchangeable, and many programmers treat them as if they were the same thing. At the hardware level, however, they compile down to noticeably different instruction sequences with different performance characteristics. This article compares the two approaches using RISC-V assembly to show exactly why pointer-based code is often faster.

Arrays vs PointersPointer ArithmeticRISC-V Address Calculation

~3 دقیقه مطالعه · آخرین به‌روزرسانی ۱۵ شهریور ۱۴۰۵

Why This Comparison Matters

In high-level C code, walking through an array using index notation and walking through it using a pointer both look natural and often produce the same logical result. But a compiler translates each style into a distinct pattern of RISC-V instructions, and the resulting hardware work is not identical. Understanding this difference is a common source of insight into why certain coding styles run faster in practice.

The Array-Indexing Approach

Consider a simple loop that clears every element of an array using index notation:

void clear1(long array[], long size) {
    for (long i = 0; i < size; i += 1) {
        array[i] = 0;
    }
}

To translate array[i] into a memory access, the processor must, on every single loop iteration, recompute the memory address by multiplying the index i by the size of each element, then adding that result to the array's base address:

Loop:
bge i, size, Exit
slli t0, i, 3
add t1, array, t0
sd x0, 0(t1)
addi i, i, 1
jal x0, Loop
Exit:

Notice the extra slli (shift left, used here to multiply by 8 bytes) and add instructions needed on every iteration purely to compute the address from the index.

The Pointer-Based Approach

Now consider the same logic written using pointer arithmetic instead of indexing:

void clear2(long *array, long size) {
    long *p;
    for (p = &array[0]; p < &array[size]; p = p + 1) {
        *p = 0;
    }
}

Here, the compiler can generate a loop where the pointer itself is directly incremented by a fixed amount each iteration, without ever recalculating an address from an index:

Loop:
bge p, end_p, Exit
sd x0, 0(p)
addi p, p, 8
jal x0, Loop
Exit:

This version eliminates the multiplication step entirely, since the pointer already holds a real memory address and only needs a simple fixed addition to move to the next element.

Why the Difference Exists

An array index is just a number that must be converted into an address every time it is used, requiring a multiplication by the element size on each access. A pointer, in contrast, already is an address, so advancing it to the next element only requires adding the element's fixed size once, rather than recomputing a product from scratch.

Practical Takeaway

Modern optimizing compilers are often capable of automatically transforming index-based loops into pointer-based ones internally, a process called Strength Reduction, which can eliminate this performance gap without the programmer needing to rewrite the code manually. However, understanding the underlying hardware difference explains why, historically, and in cases where a compiler cannot safely perform this optimization, pointer-based iteration has been recommended for performance-sensitive code.

نوشته و پژوهش‌شده توسط دکتر شاهین صیامی

مقالات مرتبط

Common Misconceptions About Parallel Computing and the Book's Final Lessons

After covering everything from thread-level parallelism to warehouse-scale computing, it is worth correcting persistent misconceptions about parallel systems that even experienced engineers sometimes hold. This article addresses common fallacies about scaling and parallel hardware, then closes out the parallel processing chapter by tying together the full journey from a single instruction to a building full of cooperating machines.

ادامه

Real Stuff: Benchmarking CPUs Against GPUs and Multiprocessor Matrix Multiply

Comparing a CPU and a GPU fairly requires a model that accounts for both computational throughput and memory bandwidth limits together. This article introduces the roofline model used to compare real hardware like the Intel Core i7 and NVIDIA Tesla GPU, then shows how matrix multiplication is accelerated across multiple processors as the final practical application of this chapter's parallel concepts.

ادامه

Benchmarking Multiprocessors and Modeling Parallel Performance

Measuring the performance of a parallel system requires different tools and metrics than measuring a single-core processor. This article covers the specialized benchmarks used to evaluate multiprocessor systems, explains how to model scaling behavior as more processors are added, and revisits Amdahl's Law in the context of real-world performance measurement.

ادامه

Cluster Networking: Connecting to the World Outside

A cluster of machines is only useful if it can communicate efficiently both internally and with the outside world. This article covers the networking layers involved in cluster communication, the tradeoffs between latency and bandwidth at scale, and how clusters connect to external networks and users.

ادامه

Clusters, Warehouse-Scale Computers, and Network Topologies

Beyond a single chip, parallelism extends to entire buildings full of independent computers working together. This article explains the shift from shared memory multiprocessing to clusters of separate machines, introduces the concept of warehouse-scale computing, and covers the network topologies that connect these independent machines efficiently.

ادامه

An Introduction to GPUs: Massive Parallelism for Data-Heavy Workloads

A GPU takes the SIMD idea covered earlier in this series to an extreme scale, running thousands of lightweight threads simultaneously to process massive amounts of independent data. This article explains why GPUs are architecturally so different from CPUs, how their thread execution model works, and what kinds of workloads benefit most from this design.

ادامه