quickest way to initiialize a int array to 0?

Hi,

have class A with
int array[100];

in const of A, try to initialize array as all 0, as
A()
{
for(int i =0; i < 100;i++)
array[i] = 0;
}

is there a better way to do it?
thanks
int array[100] = {0};
sucker... :-)
I wrote that until I read the question again..
C++ solutions:

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

// assuming vector instead of C-array: std::vector<int> array;
A() : array( 100, 0 ) {}

int array[100] = {0}; will not work in my case.
memset is really nice. but is that more efficient than running a loop?

thanks
like this one line c++ way
std::fill( array, array + sizeof( array ), 0 );
could underlying implementation is just as memset,put?
thanks
I doubt it chrisben. fill is a template function which is supposed to avoid the security problems of memset. I'd be appalled if std::fill used memset. Morever I believe that it would be impossible.

There is a critical difference between std::fill and memset that is very important to understand. std::fill sets each element to the specified value. memset sets each byte to a specified value. In the case of filling values with 0 it doesn't matter much. If you wanted to initialize each element to a non-zero value memset isn't an option. It just wouldn't work. Also memset requires that you specify the array size as a number of bytes rather than number of elements. For non-char arrays memset requires that you calculate the number of bytes where std::fill is more intuitive.

In fact the examples for std::fill in the earlier posts are incorrect. It should be:
std::fill( array, array + 100, 0 );

sizeof returns number of bytes not number of elements and would result in undefined behavior because std::fill would continue filling beyond the end of the integer array. sizeof would return 400 instead of 100 assuming that the int is 4 bytes (it probably is). Therefore it would cause std::fill to write 300 integers past the end of the array. I also recommend using a constant for defining the array size so that if the array changes you can update one line of code without worrying about creating defects.
Last edited on
Wouldn't a better solution then be: std::fill(array, array + sizeof(arraY)/(int), 0);?

EDIT:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
#include <algorithm>

int main()
{
    const int anInt = sizeof(anInt);
    int index = 0;
    int array[100];
    std::fill(array, array+sizeof(array)/anInt, 0);
    
    for(int i=0;i<100; i++)
      std::cout << i+1 << ": " << array[index++] << "\n";
    
    std::cin.get();
    return 0;
}
Last edited on
@mcleano - your solution is better than the ones that are wrong but not the best of all possible in my opinion. Yours correctly calculates the number of elements but you don't need to do that at all.

Why would I want to risk messing up the calculation of number of elements when a constant should have been used to define the array in the first place? Using a constant is also more efficient. Calculating the number of elements just adds risk in my opinion. Also, the sizeof operator may not always work with arrays. It works in that specific example but not on pointers to arrays. Many people seem to forget this and attempt to use sizeof on dynamic arrays or on arrays passed to functions and then wonder why they end up with undefined behavior. In the original question the array was a class member defined with a hard coded magic number. The best solution is to define a constant and use the constant wherever you need the array size.
std::fill(myArray, myArray + SIZE_MYARRAY, x); // where x is substituted with some value

Another reason that std::fill is better is that it is a template and can work with arrays of any type and can initialize each element to any value including but not limited to zero. For arrays of objects you can use the constructor call as the third argument to construct a copy of an object used to assign to each element.
thank you all for the input. I got more than what I want :). really appreciate the help from everyone.

I actually have three arrays to initialize at the same time, all with same size. In this case, I am wondering maybe it is more "efficient" to use a for loop instead of call fill three times. However, with current machine, I guess simpler syntax is more appealing.
Topic archived. No new replies allowed.