On 14Dec2012 16:57, Dennis Lee Bieber <wlfr...@ix.netcom.com> wrote: | On Fri, 14 Dec 2012 12:42:27 +0100, Christian Heimes | <christ...@python.org> declaimed the following in | gmane.comp.python.general: | > | > To be fair, memcpy() is a pretty simple function. It can be implemented | > in just about two lines of C code plus five lines of boiler plate. It | > shows, if you have very basic understanding about memory layout and | > pointer arithmetics. | > | > You have to translate something like | > | > def memcpy(dest, src, n): | > for i in xrange(n): | > dest[i] = src[i] | > | > into C code. Here is a rather nonperformance solution. It copies just | > one byte per cycle. A better implementation could copy 4 or 8 bytes at | > once and handle the tail in a switch statement. | > | > void *memcpy(void *dest, const void *src, size_t n) | > { | > char *destptr = (char*)dest; | > char *srcptr = (char*)src; | > while (n--) { | > *destptr++ = *srcptr++; | > } | > return dest; | > } | > | > destptr and srcptr are the memory addresses of a byte (char). *destptr | > is the value at a specific memory location. The code in the loop copies | > the value at address srcptr to the location at destptr and then moves | > both pointers one step to the right (++). That's all. ;) | | That is ignoring the possibility of overlapping source/destination | ranges (in which one may need to copy from the end rather than the | start).
If you're going to be picky, memcpy() is not required to allow for that. That allows a high speed implementation. memmove() exists to cover the more general case. -- Cameron Simpson <c...@zip.com.au> NOTWORK: n. A network when it is acting flaky. Origin (?) IBM. - Hackers' Dictionary -- http://mail.python.org/mailman/listinfo/python-list