warn/croak on "string" "string" auto-joins
Gabor Greif
gabor@mac.com
Wed Aug 18 00:49:00 GMT 2004
Am Mittwoch den, 18. August 2004, um 02:21, schrieb Adam Monsen:
> Hi Gabor,
>
> Thanks for your input!
>
> On Wed, 18 Aug 2004 01:25:10 +0200, Gabor Greif <gabor@mac.com> wrote:
>> Adam Monsen <haircut at gmail dot com> wrote:
>>> String auto-joins can be dangerous. Is there a way to tell the
>>> compiler to warn/croak when an auto-join occurs?
>>>
>>> For instance, find the bug in this code:
>>>
>>> char *months[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul",
>>> "Aug" "Sep", "Oct", "Nov", "Dec"};
>>>
>>> If not, I'd like to suggest a way to ask the compiler to strictly
>>> check for this.
>>
>> The compiler cannot shield you from all programmer errors.
>
> Agreed.
>
>> This would have helped early and is good practice:
>>
>> char *months[12] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul",
>> "Aug" "Sep", "Oct", "Nov", "Dec"};
>>
>> --> high probability warning
>
> I'm not sure I understand, what does "high probability warning" mean?
> I get no errors/warnings from the following:
>
> #include <stdio.h>
> int main( void )
> {
> char *months[15] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul",
> "Aug" "Sep", "Oct", "Nov", "Dec"};
> printf("7: %s\n", months[8]);
> return 0;
> }
>
> compiled with 'gcc -Wall -pedantic -ansi', using GCC 3.3.3.
Hmm, I could have sworn that a mismatch in array dimensions
does emit a warning (too many initializers do error out "como").
Looks like this is not the case. Sorry for bothering you :-(
>
>> Cheers,
>>
>> Gabor
>>
>> PS: I often assemble longer (format) strings from smaller ones
>> by means of macros, so if this ever to become a warning I'd
>> prefer to exclude macro-expanded occurrences.
>
> But you currently have no use for the warning, correct? Or, do you
> feel it is useful? I'm confused.
No use for the warning, as I have never made this particular error,
but it does indeed look hard to debug. So if it occurs in the source
like in your example I would not mind to be warned by the compiler.
However in case the concatenation comes from macro expansion,
warning on it would be bad.
Gabor
>
> --
> Adam Monsen <adamm@wazamatta.com>
> http://adammonsen.com/
More information about the Gcc
mailing list