8

Can I be certain the following code will work?

int sum_array(int *array, size_t size)
{
  int i;
  int sum = 0;
  for (i=0;i<size;i++)
    sum += *(array+i);
  return sum;
}

int main()
{
  int two_d_array[4][3] = {{1,2,3}, {4,5,6}, {7,8,9}, {10,11,12}};
  int sum = sum_array(&two_d_array[0][0], 12);
  printf("%d", sum);
}

While it makes sense for a 4-by-3 array to be laid out in memory exactly as a 12 element array, is it guaranteed? Since I seem to be cheating the type system, I am not so sure something could go wrong (for example, padding being added to int[3]).

Bonus points if you can elaborate on what happens if I use something other than ints in my array, and provide the relevant quote from the standard.

7
  • 3
    I'm trying to find the dupe for this. Pretty sure the conclusion is it is technically UB Mar 26, 2019 at 13:21
  • 2
    @NathanOliver I've also seen answers that say that it is technically not UB.
    – eerorika
    Mar 26, 2019 at 13:22
  • 1
    Franco, it might be easier to answer this question if you specify a specific version of the standard if you don't actually care about every standard version. (In practice, I find "compilers don't break, yet this is UB" to only be of concern in the latest standard version, because existing compilers aren't going to say "I know, back in C++14 this was UB, so lets add some optimizations around that UB which we disable in C++17") Mar 26, 2019 at 13:28
  • 1
    Seems like this paragraph indicates that i++ is undefined behavior once you reach the end of the first array. I'm not sure if anything contradicts it for the case of arrays of arrays. Mar 26, 2019 at 13:29
  • 3
    Looks like dupe closing of this and this would answer the OP's question. Mar 26, 2019 at 13:33

1 Answer 1

7

for example, padding being added to int[3]

There's no danger of that. Arrays are guaranteed to not have padding. The memory layout itself is guaranteed, while the pointer arithmetic is technically undefined, as per this quote mentioned in the comments: expr.add/4 Thus:

Is 2D array (T[N][M]) traversal by pointer (T*) guaranteed to work?

Technically not according to my understanding of the standard, unfortunately.


Here is a version of standard std::accumulate that iterates the innermost elements of a multi dimensional array of any dimension count as if it were a flat array:

template<class T, unsigned size, class BinOp = std::plus<>>
auto arr_accumulate(
    T (&arr)[size], std::remove_all_extents_t<T> init = {}, BinOp op = {})
{
    if constexpr (std::is_array_v<T>) {
        return std::accumulate(std::begin(arr), std::end(arr), init,
            [&](const auto& l, const auto& r) {
                return arr_accumulate(r, l, op);
            }
        );
    } else {
        return std::accumulate(std::begin(arr), std::end(arr), init, op);
    }
}

// usage
int sum = arr_accumulate(two_d_array, 0);

I haven't benchmarked this to see if there's any overhead. With compile time constant input such as in your example, the sum was calculated at compile time.

Another implementation; only for trivial types:

template<class T, unsigned size>
constexpr auto flat_size (T (&arr)[size]) {
    using E = std::remove_all_extents_t<T>;
    return sizeof arr / sizeof (E);
}

template<class T, unsigned size, class BinOp = std::plus<>>
auto arr_accumulate_trivial(
    T (&arr)[size], std::remove_all_extents_t<T> init = {}, BinOp op = {})
{
    using E = std::remove_all_extents_t<T>;
    static_assert(std::is_trivially_copyable_v<E>);
    std::array<E, flat_size(arr)> flat;
    std::memcpy(flat.data(), arr, sizeof arr);
    return std::accumulate(std::begin(flat), std::end(flat), init, op);
}

Not the answer you're looking for? Browse other questions tagged or ask your own question.