I am working in visual studio 2010 and I received a project where the original programmer did this about half the time inside functions and methods
TCHAR some_local_variable[size] = {0};
and the other half of the time, he did not initialize the array:
TCHAR some_local_variable[size];
I am well aware of what the first assignment does, but I was wondering if the MSVC compiler guarantees a null character at index 0 in the second case. I did test this out and the first character was set to 0 followed by garbage as expected, but I am not sure if this is guaranteed or not?
Secondly, even if a null character is guaranteed (or if it isn't, simply setting the first character to 0), is there any good reason to initialize the entire array with 0s, especially when I always do the following after any string manipulation:
some_local_variable[size-1] = 0;
My only thought is there could be a problem if the function manipulating the string did not null terminate AND the number of characters copied were less than size-1. For example, if it copied size-5 characters and did not null terminate, my termination at size-1 would potentially expose garbage in size-4 to size-2. However, I don't think is a problem with the majority of standard library or Win32 functions.