Re: Is this type of char array initization legal?
On 2008-10-24 13:35:25 -0400, Salt_Peter <pj_hern@yahoo.com> said:
On Oct 24, 12:22??pm, DomoC...@gmail.com wrote:
the code below will compile in visual c++ 2003, but im not sure its
valid.
unsigned char myString[200] = "";
after this line executes, all the bytes within myString are indeed set
to '0's' ??but is this really valid c++ or c? ??where can I find out ho
w
this is implemented?
Sounds like you see the results of a program in debug mode.
In which case, the debug mode needs further thought. Initializing to
all zeros is a bad idea when debugging, because the result looks like
good data.
The string
literal "" is actually {'\0'} so everything after myString[0] has an
undeterminate value. Nobody cares whether some compiler initializes or
not the rest of the array with some magic value, there is no
requirement that says it must.
Im concerned because I had a 3rd party library wrapper which was
crashing, and I was able to alleviate the crash by changing the
initialization method from the above to ...
unsigned char myString[200];
memset( myString, 0, sizeof( myString ) );
any guidance is greatly appreciated!
-Velik
An array has a special initializer that looks like so:
unsigned char myString[200] = {'a'};
which initializes all elements with 'a' in this case.
This is just straight aggregate initialization, and it initializes all
values after the end of the initializer to 0, not to the value of the
initializer.
--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)