1

So C/C++ arrays don't know about their length, right? But then how can the function sizeof(array) work and give us the proper size in bytes when it shouldn't be able to know the number of elements in the array?

4
  • ... An example would be useful Commented Apr 5, 2014 at 8:17
  • 1
    It only knows it when you specify a size at declaration: int pArray[10] for example. In all other cases, an array is just a pointer to the first element. Commented Apr 5, 2014 at 8:17
  • 4
    In scope they were created in, they do. It's when you pass them to functions they decay to pointers to their first element and the information about the size is lost. Commented Apr 5, 2014 at 8:17
  • 1
    sizeof(array)/sizeof(*array) Commented Apr 5, 2014 at 8:32

2 Answers 2

7

So C/C++ arrays don't know about their length, right.

Your assumption is wrong. With the exception of variable length arrays introduced in C99, arrays in both C and C++ have a size that is known in compile time. The compiler knows their size.

Your confusion is probably because there are times when array names decay into a pointer to its first element (like when passed as function argument), it's true that the size information is lost here.

But when sizeof is used on an array, the array is not converted to a pointer. This is your other confusion: sizeof is not a function, it's an operator.

Sign up to request clarification or add additional context in comments.

72 Comments

@user2341104 What? No, not in C/C++. In particular, with the exception of variable length arrays, sizeof is a compile time operator, its operand is not even evaluated.
@user2341104: your comment is riddled with misunderstandings of the small subtleties of the language. e.g.: operators aren't exactly functions. if you think so, try to overload &&.. trust Yu, he's telling you the truth.
@user2341104: Indeed, in C++ overloaded operators are functions. But the built-in operators aren't; and you can't overload the sizeof operator in any case. sizeof isn't a function, and doesn't behave like one.
@user2341104: But sizeof is fundamentally different to a function; the only similarity is that both a function call and a sizeof expression are expressions. Its operand can be either an expression or a type specifier; and in the former case it doesn't evaluate the operand. Calling it a function when it behaves fundamentally differently is much more confusing than calling it what it is.
If you think of it as a function in a mathematical sense (input something and get something else in return), then yes, operators are functions. But the term function in C++ has a second meaning: usually it implies certain stack mechanisms and some operators are totally unrelated to this concept (can you get a function pointer to point to the sizeof function?). But I'm sure there are already lengthy discussions about this on SO and I think we should stop this here in the comments.
|
1

I will quote the relevant portions of C99 standard. §6.5.3.4 ¶2 says

The sizeof operator yields the size (in bytes) of its operand, which may be an expression or the parenthesized name of a type. The size is determined from the type of the operand. The result is an integer. If the type of the operand is a variable length array type, the operand is evaluated; otherwise, the operand is not evaluated and the result is an integer constant.

It also says in the same section §6.5.3.4 ¶1

The sizeof operator shall not be applied to an expression that has function type or an incomplete type.

About the array type, §6.2.5 ¶20 says

An array type describes a contiguously allocated nonempty set of objects with a particular member object type, called the element type. Array types are characterized by their element type and by the number of elements in the array.

It again says in §6.2.5 ¶22

An array type of unknown size is an incomplete type.


So to summarize the above, the size of an array is known to the compiler (determined using sizeof operator) when you also specify the size of the array, i.e, when it's a complete type.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.