cvs commit: hugs98/src errors.c script.c hugs98/src/winhugs General.c IORemap.c Winhugs.h

ross 2005/10/04 08:26:38 PDT Modified files: src errors.c script.c src/winhugs General.c IORemap.c Winhugs.h Log: from Neil Mitchell: Factor out printing of filename links for WinHugs, and also abbreviate them using {Hugs} or ., expanding them again in editor loading. Revision Changes Path 1.9 +2 -9 hugs98/src/errors.c 1.16 +1 -3 hugs98/src/script.c 1.2 +12 -1 hugs98/src/winhugs/General.c 1.4 +35 -6 hugs98/src/winhugs/IORemap.c 1.16 +1 -0 hugs98/src/winhugs/Winhugs.h

Hi,
I noticed you changed:
FileName = &FileName[5]
to
FileName += 5
I realise on some bad C compilers the second one may be more
efficient, but the first has the advantage that when converting from
ASCII to Unicode it still works, whereas the second one corrupts the
string. Because of this, I always write using the first one.
Thanks
Neil
On 10/4/05, Ross Paterson
ross 2005/10/04 08:26:38 PDT
Modified files: src errors.c script.c src/winhugs General.c IORemap.c Winhugs.h Log: from Neil Mitchell: Factor out printing of filename links for WinHugs, and also abbreviate them using {Hugs} or ., expanding them again in editor loading.
Revision Changes Path 1.9 +2 -9 hugs98/src/errors.c 1.16 +1 -3 hugs98/src/script.c 1.2 +12 -1 hugs98/src/winhugs/General.c 1.4 +35 -6 hugs98/src/winhugs/IORemap.c 1.16 +1 -0 hugs98/src/winhugs/Winhugs.h _______________________________________________ Cvs-hugs mailing list Cvs-hugs@haskell.org http://www.haskell.org/mailman/listinfo/cvs-hugs

On Wed, Oct 05, 2005 at 11:19:24AM +0100, Neil Mitchell wrote:
I noticed you changed:
FileName = &FileName[5]
to
FileName += 5
I realise on some bad C compilers the second one may be more efficient, but the first has the advantage that when converting from ASCII to Unicode it still works, whereas the second one corrupts the string. Because of this, I always write using the first one.
I believe that in C the two are completely equivalent. (Is that not so in VC++?) I just have a prejudice that &arr[n] should be used when one means a pointer to a single element, and arr+n when one wants a pointer to the array from that position (even though in C they're the same thing). BTW, I also changed the argument to itoa(); hope that's OK.

I believe that in C the two are completely equivalent. (Is that not so in VC++?) I just have a prejudice that &arr[n] should be used when one means a pointer to a single element, and arr+n when one wants a pointer to the array from that position (even though in C they're the same thing).
As far as I was aware, if they operate on char*, they are equivalent, since sizeof(char) = 1. However, if the code is ever switched to using unicode characters, which makes it a short*, then b = &b[5] is equivalent to b += 5 * sizeof(short), which is b += 10. For the moment they are the same, but since unicode support is a distinct possibility in the future its best to keep it as easy as we can.
BTW, I also changed the argument to itoa(); hope that's OK. Fine, its not a problem now, I was just making everyone aware that unicode adds certain concerns.
Thanks Neil

On Wed, Oct 05, 2005 at 11:45:27AM +0100, Neil Mitchell wrote:
I believe that in C the two are completely equivalent. (Is that not so in VC++?) I just have a prejudice that &arr[n] should be used when one means a pointer to a single element, and arr+n when one wants a pointer to the array from that position (even though in C they're the same thing).
As far as I was aware, if they operate on char*, they are equivalent, since sizeof(char) = 1. However, if the code is ever switched to using unicode characters, which makes it a short*, then b = &b[5] is equivalent to b += 5 * sizeof(short), which is b += 10. For the moment they are the same, but since unicode support is a distinct possibility in the future its best to keep it as easy as we can.
In C, p[n] is defined as *(p+n) (see [1] 6.5.2.1), with addition of pointers and integers defined as advancing the pointer by n array elements (6.5.6). So (p = &p[5]) === (p = &*(p+5)) === (p = p+5) === (p += 5) whatever the value of sizeof(*p). [1] ISO/IEC 9899:1999 http://www.nirvani.net/docs/ansi_c.pdf, but this part was there from the beginning.

In C, p[n] is defined as *(p+n) (see [1] 6.5.2.1), with addition of pointers and integers defined as advancing the pointer by n array elements (6.5.6). So
(p = &p[5]) === (p = &*(p+5)) === (p = p+5) === (p += 5)
whatever the value of sizeof(*p).
[1] ISO/IEC 9899:1999 http://www.nirvani.net/docs/ansi_c.pdf, but this part was there from the beginning.
You learn something new every day :) Neil
participants (2)
-
Neil Mitchell
-
Ross Paterson