Using small memory model functions on huge arrays (was See below...)

D'Arcy J.M. Cain darcy at druid.uucp
Tue May 29 05:09:35 AEST 1990


In article <1990May28.000424.20473 at druid.uucp>
darcy at druid.UUCP (D'Arcy J.M. Cain) (Me) writes:
>In article <90052709560067 at masnet.uucp>
>simon.ewins at f664.n250.z1.fidonet.org (simon ewins) writes:
>>All I really want to do is find a way to use a small memory model to access
>>an array of 2000 strings of 81 bytes in length!
>You have to in effect write your own functions.  For example, the strcpy above
>can be written as:
>#define strcpy(dest, src) {char *s = src, *d = dest; while (*s) *d++ = *s++;}
>Of course you would use a name like hstrcpy or something so as not to interfere
>with strcpy and you have to watch for side effects but you probably get the
>idea.  You can also write it inline if it doesn't happen in too many places.

Some sent me e-mail pointing out 2 problems with this macro.  There is no
return value and it doesn't copy the null byte.  I don't see the first as a
real problem since as I go on to explain that it shouldn't be called strcpy
then it doesn't have to act exactly like it.  The second is a problem.  It
is easily fixed by changing to a do ... while loop.  (do *d++ = *s while
(*s++)).

However he went on to suggest that I really should have used:

char *strcpy(dest, src)
   char *dest, *src;
   {
   char *s = src;

   while (*d++ = *s++) ;
   return src;
   }

Which doesn't work because it ignores the fact that the arguments may be
different sized pointers.  Of course it is really easy to criticize code
from someone else and as soon as I realized what was wrong with that code
I realized that it applied to my code as well.  So I'll try again:

#define mixstrcpy(d, s)  { int k = 0; do d[k] = s[k]; while (s[k++]);}

At that point it is probably just as easy to put it inline any way.  I
tested the above on Unix but it should work on brain dead OS's as well.

-- 
D'Arcy J.M. Cain (darcy at druid)     |   Government:
D'Arcy Cain Consulting             |   Organized crime with an attitude
West Hill, Ontario, Canada         |
(416) 281-6094                     |



More information about the Comp.lang.c mailing list