1

Excerpt from TopCoder article:

The expression sizeof(data)/sizeof(data[0]) returns the size of the array data, but only in a few cases, so don’t use it anywhere except in such constructions.(C programmers will agree with me!)

To get array size, I've been using this expression sizeof(data)/sizeof(data[0]) all the time for all primitive types.

Does anyone know about any such case in which the above expression should not be used?

10
  • Use std::vector or std::array and you won't care about when that won't work. Commented Dec 29, 2016 at 19:14
  • yeah, I use them in C++. But many times, I code in C as well, where I use this expression. Is this problem language specific? Commented Dec 29, 2016 at 19:16
  • I think it isnt typesafe Commented Dec 29, 2016 at 19:16
  • 2
    Maybe this question would be informative as to why the author makes that blanket assessment. Commented Dec 29, 2016 at 19:18
  • 1
    @MikelF (from the revision history): C++ that looks like C is still C++, and this question is about C++. Transforming it into a C question would also invalidate Dietmar Kühl's answer. Commented Dec 29, 2016 at 20:42

3 Answers 3

4

If data were declared like so:

int *data;

And then space for it allocated like so:

data = malloc( NUM_ELEMENTS * sizeof(int) );

Then your technique will not work, because sizeof(data) is the size of a pointer, not the content of the array.

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

Comments

4

The sizeof approach compiles but doesn't work when giving it a pointer or an array of indeterminate size. Just use the proper C++ approach:

template <typename T, std::size_t N>
constexpr std::size_t size(T(&)[N]) {
    return N;
}

Using size() on an array works correctly. It will be a compile-time error to use it in a case where it is not applicable, e.g., on a pointer.

5 Comments

Or even easier since c++11 std::extent<decltype(array)>::value
Thanks.. What is this expression called T(&)[N] i.e. & inside brackets as argument? Seeing it first time. What term I should google, any hints?
@SebTu: somehow I think size(array) is expressing more clearly what it intended and is easier to type than std::extent<decltype(array)>::value.
@SauraSaha: that's just a reference to an array type. The array type is T[N]. Simply writing T[N]& is illegal and T&[N] would be an array of references (which is also illegal). Essentially the reference (or pointer) goes where the name goes but it needs appropriate parenthesis to disambiguate what is meant. Hence T(&)[N]. If it had a name which isn't needed for the function it would be T (&array)[N].
@SauravSahu take a look at the clockwise spiral rule. Helped me a lot many times. Especially do not forget to consider the third rule : 3. Always resolve anything in parenthesis first!
0

The sizeof technique work correction for static array, it will not have any issue. As mentioned in the above for dynamic array and pointer data it will not work correctly.

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.