13

I simply want to set a complete array back to 0. Something like a "reset" method.
I know that I can use something like this to initalize an array to zero:

int array[100] = {0};     //possible since c++11

but I am not sure to reset it. Something like array[100] = {0}; only sets the 100-element to 0. I know I can do it with a for loop, but there has to be a better way.
I am not allowed to use memset cause of the coding guideline.

CodingCat
  • 181
  • 1
  • 1
  • 9
  • 4
    Use `std::array` or `std::vector` – Slava Dec 15 '17 at 14:20
  • 2
    "Something like array[100] = {0}; only sets the 100-element to 0" no it is not, that's UB it can do anything. – Slava Dec 15 '17 at 14:21
  • The example you give is actually original C, and in original C there is very little help for arrays, except memset. Would it be acceptable to wrap a template round your memset? – Gem Taylor Dec 15 '17 at 14:21
  • @GemTaylor Sadly it is not. Realy? I thought that the array initalization is part since c++11 and not possible in c. I might be wrong here. – CodingCat Dec 15 '17 at 14:24
  • for reference, anti-dupe: [Reset C int array to zero : the fastest way?](https://stackoverflow.com/questions/9146395/reset-c-int-array-to-zero-the-fastest-way) – underscore_d Dec 15 '17 at 14:27
  • 1
    `std::array` is your friend. It's a safer array. It's just a normal built-in array underneath so there's no memory allocation overhead. – Nikos C. Dec 15 '17 at 14:29
  • _I am not allowed to use memset cause of the coding guideline._ but that guideline allows you to use simple arrays? It doesn't tell you what to use instead of memeset? –  Dec 15 '17 at 14:32
  • @manni66 Correct and correct. In some projects you get predefined datatypes like "myint16", shit operators for those types, no std support and tons of restrictions. Everyday life of a programmer and I cant change it. – CodingCat Dec 15 '17 at 14:38
  • @CodingCat no, not everyday life of a programmer. Or, to speak with Andrei Alexandrescu: call your headhunter. –  Dec 15 '17 at 14:41
  • C++11 brought in the ability to use C-style initialisation on C++ objects. C already had `char strArray[] = "Hello world";` since the 70s! – Gem Taylor Dec 15 '17 at 14:57

2 Answers2

29

You might use std::fill:

std::fill(std::begin(array), std::end(array), 0);
Jarod42
  • 203,559
  • 14
  • 181
  • 302
23

For a C style array such as int array[100] you can use std::fill as long as array is an array. A pointer to the array will not work.

std::fill(std::begin(array), std::end(array), 0);

If you are using a pointer to the first element, you must supply the size of your array yourself.

std::fill(array, array + size, 0);

In C++, it's recommended to use std::array instead of C style arrays. For example, you could use std::array<int, 100> foo; instead of int foo[100]; std::array always knows its size, doesn't implicitly decay to a pointer and has value semantics. By using std::array you can simply reset the array with :

foo.fill(0);

or

foo = {};
François Andrieux
  • 28,148
  • 6
  • 56
  • 87
  • How do you know the default value assigned by `foo = {}`? – Phlucious Dec 07 '18 at 18:37
  • 1
    @Phlucious That's [aggregate initialization](https://en.cppreference.com/w/cpp/language/aggregate_initialization). If the list is empty or shorter than the number of elements, the remaining elements are [value initialized](https://en.cppreference.com/w/cpp/language/value_initialization). For `int` type elements, that means initializing them to zero. – François Andrieux Dec 07 '18 at 18:41
  • 1
    If you want to create an `std::array` initialized to zero you can create the array as ```c++ std::array row({0}); ``` because the object in its construction calls `std::fill_n`, that for C++20, I don't know for previous standards. – sɪʒɪhɪŋ βɪstɦa kxɐll Aug 30 '21 at 04:45