Mail Archives: djgpp/1998/05/18/11:53:15
On Mon, 18 May 1998, Pierre Muller wrote:
> And there I discovered a bug :
> sizeof(try_char) returned 36
> but this is not correct
> because
> static char try_char[] = "abcdefghijklmnopqrstuvwxyz012345789";
> is only 35 bytes long (notice the missing 6 in the digits !!)
No, this is perfectly correct: sizeof includes the terminating null
character, that's why the code says "sizeof(try_char)-1". (The
omission of 6 is probably a typo, but it shouldn't do any real harm in
this case.)
> so the statement
>
> do
> {
> if (idx <= 0)
> return -1;
> *pbase = try_char[--idx];
> /* bug sizeof(try_char) returns 36 instead of 35)
> so the first is 0 !! */
No. idx is initialized like this:
int idx = sizeof(try_char)-1;
Since sizeof returns 36, idx begins with the value of 35. So the
statement
*pbase = try_char[--idx];
actually does this:
*pbase = try_char[34];
(since --idx means "decrement first, and then use"). So there's no
problem here that I can see.
> Morality : we should not use sizeof here
> because it round up to 4 bytes but strlen I suppose
No, sizeof doesn't round up, it just gives the size of the object in
bytes. sizeof can only round up when the compiler adds padding to a
variable due to alignment considerations, but these do not apply to char
buffers.
> Adding the extra check worked for me
> replacing sizeof by strlen worked also
> reinserting the 6 digit would also work ...
I cannot believe that this solved the problem. I also don't see the
problem; I have run several different cases, just to be sure, and couldn't
get utod or dtou to fail. Please post the details as I asked for them
(below). You have left them without any response. The most important
piece of information is the exact comaand line that you use when these
programs fail.
> >There's something strange in your system setup, or in the files that you
> >are trying to convert. Please post the exact command you used to convert
> >the files, and also the shortest file that can be used to reproduce your
> >problem, including its file name.
> >
> >Please also post the output of "dir dtou.exe".
- Raw text -