Pete Becker
10/24/2008 8:47:00 PM
On 2008-10-24 16:18:56 -0400, Victor Bazarov <v.Abazarov@comAcast.net> said:
> Pete Becker wrote:
>> On 2008-10-24 12:49:25 -0400, Victor Bazarov <v.Abazarov@comAcast.net> said:
>>
>>> DomoChan@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 how
>>>> this is implemented?
>>>>
>>>> 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!
>>>
>>> It is valid. Allowed explicitly by the Standard, subclause 8.5.2.
>>>
>>> The only reason it could be crashing is if the compiler wastn't
>>> providing proper initialisation for the array. What you could do is
>>> revert this to what it was and put an assertion to see if it's indeed
>>> the problem:
>>>
>>> unsigned char myString[200] = "";
>>> #ifndef NDEBUG
>>> for (int iii = 0; iii < 200; ++iii)
>>> ASSERT(myString[iii] == 0);
>>> #endif
>>
>> Hmm, is this required if the char array has automatic storage duration?
>> I have always assumed that it wasn't, that only the characters
>> corresponding to characters in the initializer would be initialized,
>> but it doesn't seem completely clear from a quick glance at the
>> standard.
>>
>
> 8.5.1/7:
> <<If there are fewer initializers in the list than there are members in
> the aggregate, then each member not
> explicitly initialized shall be value-initialized (8.5).>>
Well, yes, that's the requirement for aggregate initialization. But
does aggregate initialization apply here? Aggregate initialization is
indicated by "a brace-enclosed, comma-separated list ...", which isn't
present here. I don't see anything in 8.5.2 [dcl.init.string] that says
that aggregate initialization is used, although the "optionally
enclosed in braces" hints that it may be.
--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)