Understanding sizeof in C++: Why Does n Not Equal 8?

When working with arrays in C++, you might encounter unexpected results related to the sizeof operator, especially when passing arrays to functions. In this blog post, we will explore a common confusion that arises when using sizeof with function parameters, particularly answering the question: Why does n not equal 8 in the function foo()?

The Problem

Let’s examine the two pieces of code that illustrate the issue:

Example 1: Function foo()

void foo(char cvalue[8])
{
    int n = sizeof(cvalue);
}

In the example above, when we call sizeof(cvalue), you might expect that n equals 8, as the array is defined with a size of 8. However, this is not the case.

Example 2: Function bar()

void bar()
{
    char cvalue[8];
    int n = sizeof(cvalue);
}

In this second example, sizeof(cvalue) does equal 8. So, why the difference?

Understanding the Concept

To understand why sizeof(cvalue) returns different values in each function, we need to clarify how arrays are treated in C and C++.

Arrays as Function Parameters

When you pass an array to a function in C or C++, you’re not actually passing the array itself. Instead, what you’re passing is a pointer to the first element of the array. The brackets used in the function parameter are merely a syntactic notation, which does not change the behavior—both of the following declarations are equivalent:

  • void foo(char cvalue[8])
  • void foo(char cvalue[])
  • void foo(char *cvalue)

In all these declarations, cvalue is interpreted as a pointer. Thus, when you call sizeof(cvalue) inside foo(), it gives you the size of the pointer, not the size of the array. On most platforms, this size is typically 4 bytes on a 32-bit system and 8 bytes on a 64-bit system, which is why n does not equal 8 in foo().

The Right Context in bar()

In contrast, inside bar(), cvalue is defined as a local array of size 8. Therefore, when sizeof(cvalue) is invoked here, it correctly reflects the size of the entire array, resulting in n equaling 8.

Key Takeaways

  • Understanding Pointer Behavior: When passing arrays as parameters, you’re actually passing a pointer, which can lead to misleading values when using sizeof.
  • Local Arrays: sizeof gives the actual size of local arrays defined within a function, providing the expected output.
  • Syntactic Sugar: The brackets in function parameters do not create array variables but rather denote that you’re handling a pointer.

Conclusion

Handling arrays in C and C++ can be tricky, especially for those new to the language. Understanding the distinction between pointers and arrays is crucial for accurate computation and debugging. Remember that when you’re inside a function, sizeof applied to a parameter assumed to be an array gives you the size of the pointer, not the size of the array you intended to pass.

Hopefully, this explanation sheds light on the sizeof operator’s behavior in relation to array arguments in functions, clarifying why n does not equal 8 in the function foo() but does in bar(). Keep these principles in mind as you code!