41

Say I want to initialize myArray

char myArray[MAX] = {0};  
char myArray[MAX] = {0,};  
char myArray[MAX]; memset(myArray, 0, MAX);  

Are they all equal or any preferred over another?

Thank you

jww
  • 97,681
  • 90
  • 411
  • 885
eugene
  • 39,839
  • 68
  • 255
  • 489
  • Doesn't only the last one initialize *all* of the elements, not just the first one? – edA-qa mort-ora-y Apr 08 '11 at 06:55
  • 2
    No, they all initialize every element. In the first two, the first element is explicitly zero and the rest are implicitly zero; in the last one, they are all explicitly set to zero. – user541686 Apr 08 '11 at 06:56
  • 1
    @edA: there is no such thing as "partial initialization" in C. An object is either totally initialized or completely uninitialized. – pmg Apr 08 '11 at 08:36
  • 2
    @Mehrdad - This is incorrect. As an example, try this: unsigned int test[32] = {0xDEADBEEF}; // The array will only contain one element with 0xDEADBEEF in it – Brian Vandenberg Sep 27 '11 at 21:49
  • 2
    @BrianVandenberg: How does that contradict what I said? – user541686 Sep 27 '11 at 22:36

6 Answers6

48

Actually, in C++, I personally recommend:

char myArray[MAX] = {};

They all do the same thing, but I like this one better in C++; it's the most succinct. (Unfortunately this isn't valid in C.)

By the way, do note that char myArray[MAX] = {1}; does not initialize all values to 1! It only initializes the first value to 1, and the rest to zero. Because of this, I recommend you don't write char myArray[MAX] = {0}; as it's a little bit misleading for some people, even though it works correctly.

user541686
  • 205,094
  • 128
  • 528
  • 886
25

They are equivalent regarding the generated code (at least in optimised builds) because when an array is initialised with {0} syntax, all values that are not explicitly specified are implicitly initialised with 0, and the compiler will know enough to insert a call to memset.

The only difference is thus stylistic. The choice will depend on the coding standard you use, or your personal preferences.

Sylvain Defresne
  • 42,429
  • 12
  • 75
  • 85
  • 4
    I think that you should state the difference between {0} and {}. Although the result is the same, the meaning is not. Also, without that info, it could lead someone to think that {1} will initialise all to 1, which is wrong (as stated in @Mehrdad answer). – amfcosta Mar 10 '16 at 12:47
6

I think the first solution is best.

char myArray[MAX] = {0};  //best of all
Nawaz
  • 353,942
  • 115
  • 666
  • 851
1

Either can be used

But I feel the below more understandable and readable ..

  char myArray[MAX]; 
  memset(myArray, 0, MAX);
Jhaliya - Praveen Sharma
  • 31,697
  • 9
  • 72
  • 76
1

Assuming that you always want to initialize with 0.

--> Your first way and 2nd way are same. I prefer 1st.

--> Third way of memset() should be used when you want to assign 0s other than initialization.

--> If this array is expected to initialized only once, then you can put static keyword ahead of it, so that compiler will do the job for you (no runtime overhead)

iammilind
  • 68,093
  • 33
  • 169
  • 336
-1

You can use also bzero fn (write zero-valued bytes)

#include <strings.h>
void bzero(void *s, size_t n)

http://linux.die.net/man/3/bzero

Bogdan Ruzhitskiy
  • 1,167
  • 1
  • 12
  • 21