Why sizeof(pointer) ≠ sizeof(array)

Same data. Different meaning.
One knows the memory. The other only knows the address.

1. The Illusion

In low-level programming, arrays and pointers often appear interchangeable. In many expressions, an array decays into a pointer to its first element.

Because of this behavior, developers sometimes assume that an array and a pointer behave the same way in all contexts. They do not. One of the most common mistakes occurs when using sizeof.

2. What sizeof Actually Measures

The sizeof operator returns the number of bytes occupied by a type or object.

When applied to an array, it returns the total size of the entire array in memory. When applied to a pointer, it returns the size of the pointer itself, not the memory it points to.

In most modern systems, a pointer is simply an address stored in a register or memory location. This means sizeof(pointer) returns the size of that address.

3. Why This Causes Bugs

Developers often use sizeof to determine the number of elements in an array. This works correctly only when the variable is actually an array.

However, once an array is passed to a function, it decays into a pointer. At that point, sizeof no longer reflects the original array size. Instead, it returns only the size of the pointer.

In embedded systems, this can lead to:
  • Incorrect buffer sizes
  • Partial memory operations
  • Silent memory corruption

4. Memory Reality

An array occupies contiguous memory and stores all its elements directly. A pointer, however, stores only a memory address.

The pointer has no knowledge of:
  • how large the original array was
  • how many elements exist
  • where the memory boundary ends

Because of this, sizeof(pointer) cannot determine array size.

5. Practical Insight

In embedded development, incorrect assumptions about memory size can cause serious issues such as buffer overflows, corrupted data, or unstable firmware behavior.

Understanding the distinction between arrays and pointers is essential when writing memory-sensitive code, especially when interacting with hardware buffers, DMA regions, or communication stacks.
Arrays know their size.
Pointers only know where memory begins.