博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
自己动手实现C语言函数memcpy()
阅读量:4283 次
发布时间:2019-05-27

本文共 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/

你可能感兴趣的文章
copy constructor
查看>>
Some interesting facts about static member functions in C++
查看>>
ubuntu 如何上ptt
查看>>
C++ Can't Overload Static Function with Non-Static Function
查看>>
conversion constructor
查看>>
malloc() vs new
查看>>
What is the use of having destructor as private?
查看>>
type-cast operator
查看>>
When do we use Initializer List in C++?
查看>>
‘this’ pointer in C++
查看>>
scp command
查看>>
To Install Android Studio in Ubuntu 16.04 and Ubuntu 17.04
查看>>
how to extract code of apk file
查看>>
android 相關網址
查看>>
動態開關android log level
查看>>
how to mount /system as read/write in android?
查看>>
Android 線上的opengrok
查看>>
Android 好的文章網誌
查看>>
vim 常用指令
查看>>
Glib相關
查看>>