Initializing arrays to zero all in one go?

This is a discussion on Initializing arrays to zero all in one go? within the C++ Programming forums, part of the General Programming Boards category; Hello,
Is there a way to initialize a 2D or 1D array to zeros all at the same time?
I ...

I don't find it a good practice to initialize everything before assigning. It just does not make sense. It slows down the program execution and makes the compiler give warnings about assigned values which are never used.

Nearly every modern compiler has an option to inform you, if a variable is used uninitialized (some languages even require it).

You'd better take care about your buffer overflow. I'd use a vector instead of a raw array.

I don't find it a good practice to initialize everything before assigning. It just does not make sense. It slows down the program execution and makes the compiler give warnings about assigned values which are never used.

Nearly every modern compiler has an option to inform you, if a variable is used uninitialized (some languages even require it).

You'd better take care about your buffer overflow. I'd use a vector instead of a raw array.

I agree with you, and that's why I always compile my code with the maximum warning degree, as well as with fatal warnings.

However, Kat007 asked for a way to do it. Another alternative would be using brace-initialization:

With this code you will initialize only the first element.
memset would be a better choice.

I just checked it and you are absolutely right. I am terribly sorry for inducing other people in error.

Although I really thought that the compiler standardly assumed that since only one element was given, the same value applied to the rest. I see now that I was wrong (a simple test case will check it). Once again, I'm sorry. There really should be an operator to initialize all the elements of an array. Maybe a syntax like: int array[x]={[]=0};.

I don't find it a good practice to initialize everything before assigning. It just does not make sense. It slows down the program execution and makes the compiler give warnings about assigned values which are never used.

I totally disagree. It stops a host of errors.

It has not slown down program execution to any measurable degree in any application I have written.

There is not golden rule when it comes to initializing. It would have a significant measurable impact if you initialize a big array that it is used in heavy computations.

You can make some more specific rules for your programming tactics. Like
-Initialize arrays that are used for user input
-Initialize arrays that are not used in heavy computations
-Initialize arrays where the initial value will be the default value

If you are just creating an array, I would say leave it unitialized. If you feel that at one point you want to have a check so that a valid value is given, add the initializing code then.

Aah! So I was right after all! Except that I thought that {1} would set all to 1, which is false.

Just in case some people don't want to follow the link, here's the relevant part:

#21
If there are fewer initializers in a brace-enclosed list than there are elements or members of an aggregate, or fewer characters in a string literal used to initialize an array of known size than there are elements in the array, the remainder of the aggregate shall be initialized implicitly the same as objects that have static storage duration.

Just remember that initializing to, say, 1, isn't as simple. This sets every byte to 0. If you use 1, then every byte will be one. Since an int is (usually) 4 bytes, you'd get the integer whose 4 bytes were set to 1, which is something you probably didn't want.