kuyper wrote On 08/23/07 11:27,:
> I've run across some rather peculiar code; here are the relevant lines
> that left me confused :
>
> unsigned char cr_file[384];
> unsigned char num_char[0];
>
> Note: this declaration actually works on our compiler, and it appears
> to be equivalent to giving a length of 1. The developer inserted
> compiler options into the make file to turn off the relevant warning
> messages. Sadly, this is not the most confusing part of the code. This
> is an example of the confusing part:
>
> num_char[0] = 1;
> memcpy(&cr_file[147], num_char, 1);
>
> num_char is used only in this fashion; its value after the call to
> memcpy() has no bearing on the behavior of the program. I may be
> missing something, but it seems to me that this code is therefore
> exactly equivalent to
>
> cr_file[147] = 1;
>
> In fact, I would expect that some compilers would generate identical
> code for both ways of writing it.
>
> Am I missing something? If not, could someone at least suggest a
> plausible reason why the developer might write such bizarre code? I
> can't ask the developer, he died recently, which is how I became
> responsible for this code.
A guess: The bogus definition of num_char[0] may
actually allocate memory as would num_char[1], but has
some other bizarre effect as well. If it didn't do
something unusual the programmer would have written [1]
in the first place, instead of writing [0] and then
going to the extra work of figuring out how to turn the
error message off. The use of memcpy() instead of
`cr_file[147] = num_char[0]' or `cr_file[147] = 1' may
have something to do with whatever that weird effect is.
Guess #2: Does the code call "the" memcpy(), or some
out-of-the-blue substitute? Writing your own substitutes
for Standard library functions is a no-no, but we've
already seen that the author didn't feel held bound to
respect the Standard at all times ...
Guess #3: Somewhere in the dusty annals of the code's
ancestry you will find the word IOCCC -- or was it XYZZY?
--