Frustrated trying to be portable

Rick Schubert rns at tortuga.SanDiego.NCR.COM
Sat Feb 23 08:04:28 AEST 1991


In article <1991Feb20.175318.28496 at zoo.toronto.edu> henry at zoo.toronto.edu (Henry Spencer) writes:
>In article <4188 at skye.ed.ac.uk> richard at aiai.UUCP (Richard Tobin) writes:
>I haven't studied <stdio.h>, but you need to look more closely at <string.h>
>before making such statements. :-)  It is not possible to implement memmove(),
>in particular, in portable C.  (Hint:  think about pointer comparisons.)

I think it's been stated before that memmove() cannot be implemented in
portable C, but now that I think about it, I don't think that's strictly
true.  It IS true that the relational operators cannot be used to compare
the 2 pointer arguments to memmove() (since they may, and probably do,
point to different objects), but the equality operators may be
used to compare such pointers.  Given the prototype:
            void *memmove(void *s1, const void *s2, size_t n);
if the objects pointed to by |s1| and |s2| overlap, then either |s1|
is one of the |n| bytes pointed at% by |s2| (in which case the copy should
start at |(char *)s2 + n - 1| and proceed backward), or |s2| is
one of the |n| bytes pointed at by |s1| (in which case the copy should
start at |(char *)s2| and proceed forward).  Each of these conditions
can be determined with at most |n| pointer-equality comparisons.

I'm not necessarily recommending such an implementation; it's just that
it can be done portably.

As far as <stdio.h> is concerned, I don't think you can get very far without
some primitive I/O facilities.  Since the C language itself (outside of the
<stdio.h> library) has no I/O facilities, there is no way to do any I/O
in the <stdio.h> library without calling another <stdio.h> function or
calling a system-specific I/O routine.  Or am I missing something?

% Chris Torek: Did I use "point at" correctly?

-- Rick Schubert (rns at tortuga.SanDiego.NCR.COM)



More information about the Comp.std.c mailing list