Inside of char* string buffer initialization

Pope Kim Jun 9, 2011

I used to initialize a string buffer this way:

char temp[64];
temp[0] = 0;

A few years ago, one of my coworkers at my previous studio, Capcom Vancouver, told me the following way is better:

char temp[64] = {0, };

I don't remember why he said it was better. I've been just using this way since I'm a nice guy who trusts one's coworkers. But finally I figured it out…. well… by accident…

The other day I was doing some profile captures on Xbox 360 and I happened to see how the above code gets compiled into. Once compiled, it turns into this:

char temp[64];
memset(temp, 0, sizeof(char) * 64);

Interesting, huh? It takes a few micro secs, so not that bad, but now I think this type of initialization is not always necessary if the buffer is always filled by strcpy or similar functions right after. (so as long as it eventually becomes null-terminated)