0

I have read many answers on stackoverflow and codereview that says we should not use C style arrays with C++. What is reason for it ? If I use std::Array instead of C style array, will it impact on speed/performance ?

In my organization, entire application is written in C++. But only C style arrays are used.

1 Answers1

1

No, it will not have impact on performance. There are two reasons to use std::array instead C-like arrays:

  • Method at() that allows to get an element by index with bounds checking. Beware, operator [] doesn't have bounds checking.
  • Limited pointer arithmetic. It's much harder to shoot in the foot than with C-like arrays.
  • Same behavior as other std containers, like begin(), end(), size() methods without necessity to handle two variables: pointer and size. So, you can implement one algorithm that can work with arrays and other containers.

Thanks Thomas Sablik for clarification in comments.

  • @ThomasSablik Yes, but the problem with simple C-style arrays is that they too often decay to pointers, and then `std::begin()` and `std::end()` won't work very well. – Some programmer dude Sep 23 '19 at 11:48