views:

263

answers:

6

Is there a way to initialize an array of primitives, say a integer array, to 0? Without using a for loop? Looking for concise code that doesn't involve a for loop.

:)

+15  A: 
int array[10] = {}; // to 0

std::fill(array, array + 10, x); // to x

Note if you want a more generic way to get the end:

template <typename T, size_t N>
T* endof(T (&pArray)[N])
{
    return &pArray[0] + N;
}

To get:

std::fill(array, endof(array), x); // to x (no explicit size)

It should be mentioned std::fill is just a wrapper around the loop you're trying to avoid, and = {}; might be implemented in such terms.

GMan
Deleted my answer (yours just says the same thing but much clearer -- and I like the template solution!). I'd give you +1 but I'm out of votes... again.
Billy ONeal
@Billy: Heh, thanks. You and your votes. :) (Or lack thereof.)
GMan
@Billy: I've upvoted it, I'll grant you half of my vote if you wish :p
Matthieu M.
+5  A: 

Yes, it is possible. The initialization method depends on the context.

If you are declaring a static or local array, use = {} initializer

int a[100] = {};  // all zeros

If you are creating an array with new[], use () initializer

int *a = new int[100](); // all zeros

If you are initializing a non-static member array in the constructor initializer list, use () initializer

class C {
  int a[100];

  C() : a() // all zeros
  {
    ...
  }
};
AndreyT
Where is the zero-initialization documented? I thought one of the things in C/C++ you could NOT count on was that it would be initialized to anything at all? In fact, that's a BENEFIT of C/C++ over Java (sometimes), since that language guarantees that for certain types, whereas C/C++ doesn't take the (slight) performance hit, and gives you the memory "as-is".
Kevin
@Kevin: What do you mean? If you leave out any of the initializers above, the arrays remain uninitialized.
GMan
@Kevin: It does give you the memory "as is", unless you *explicitly ask* for zeroes. When you explicitly use initializers like `= {}` or `()`, you expliciutly ask for value-initialization, which boils down to zero-initialization for basic types. For example, if you do `new int[100]`, you get memory "as is", but if you do `new int[100]()` you get zeros. It is that `()` that makes all the difference.
AndreyT
@Kevin: It is all documented in the strandard. It is spread across several different places though: aggregates *and their initializers), `new`, and initializing bases and members.
AndreyT
+1  A: 

You can use memset if you want all your values to be zero. Also, if you're only looking to initialize to zero, you can declare your array in such a way that it is placed in the ZI section of memory.

Paul Ellis
A: 

If the number is zero you could also use memset (though this is more C-style):

int a[100];
memset(a, 0, sizeof(a));
Mongoose
A: 

double A[10] = { value }; // initialize A to value. I do not remember if value has to be compiled constant or not. will not work with automatic arrays

aaa
A: 

There are ways of concealing what you are doing with different syntax, and this is what the other answers give you - std::fill, memset, ={} etc. In the general case, though (excluding compiler-specific tricks like the ZI one), think about what needs to be done by the compiled code:

  • it needs to know where your memory block/array starts;
  • it needs to set each element of the block to the value in turn;
  • it needs to check repeatedly whether the end of the block has been reached.

In other words, there needs to be a loop in a fairly fundamental way.

Tom Smith