本文共 600 字,大约阅读时间需要 2 分钟。
memcpy指的是c和c++使用的内存拷贝函数,memcpy函数的功能是从源src所指的内存地址的起始位置开始拷贝n个字节到目标dest所指的内存地址的起始位置中。
我自己其实对于C语言不是那么熟悉,这个函数之前也没用过,但是上星期恰好面试**的时候,面试官出过相似的问题,加上自己也在上面的文章中总结过,所以直接将memmove()的实现写上了。下面的是在网上查到的版本,也就是将内存重叠问题考虑在内的优化版。
void* memcpy(void *dst, const void *src, size_t size) { char *psrc, *pdst; if (dst == NULL || src == NULL) return NULL; if (dst <= str) { psrc = src; pdst = dst; while (size--) *pdst++ = *psrc++; } else { psrc = src + size - 1; pdst = dst + size - 1; while (size--) { *pdst-- = *psrc--; } return pdst;}
转载地址:http://vmcgi.baihongyu.com/