C语言::memcpy函数功能、原型、用法及实例 - 高小调博客

C语言::memcpy函数功能、原型、用法及实例

功能

memcpy函数的功能将资源内存(src所指向的内存区域)拷贝n个字节到目标内存(dest所指向的内存区域).

(原理:将dest、src强制转换成char *,然后循环赋值n次.)

(详见:C语言::模拟实现memcpy函数)

原型

void *memcpy(void *dest,const void *src,size_t n);

(定义于<memory.h>或<string.h>)

参数

dest:任意类型的指针.

src:任意类型的指针.

返回值

最终返回dest

注意事项

memcpy函数只能比较字符串,不能比较数字等其他形式的参数.

函数实现

//Winows下memcpy函数的实现
void* __cdecl memcpy(void* dst,const void* src,size_t count){
    void* ret=dst;
	#if defined(_M_MRX000)||defined(_M_ALPHA)||defined(_M_PPC){
     extern void RtlMoveMemory(void *,const void *,size_t count);
     RtlMoveMemory(dst,src,count);
	}
	#else /*defined(_M_MRX000)||defined(_M_ALPHA)||defined(_M_PPC)*/
	/*
	*copy from lower addresses to higher addresses
	*/
	while(count--){
		*(char *)dst = *(char *)src;
		dst = (char *)dst+1;
		src = (char *)src+1;
	}
	#endif  /*defined(_M_MRX000)||defined(_M_ALPHA)||defined(_M_PPC)*/
	return (ret);
}
//coreutils中memcpy函数的实现
void* memcpy(void*destaddr,voidconst*srcaddr,size_tlen)
{
    char* dest=destaddr;
    char const* src=srcaddr;
    while(len-->0)
    {
    *dest++ = *src++;
    }
    return destaddr;
}
//Linux下memcpy函数的实现
void *memcpy(void *to, const void *from, size_t n)
{
    void *xto = to;
    size_t temp, temp1;
 
    if (!n)
        return xto;
    if ((long)to & 1) {
        char *cto = to;
        const char *cfrom = from;
        *cto++ = *cfrom++;
        to = cto;
        from = cfrom;
        n--;
    }
    if (n > 2 && (long)to & 2) {
        short *sto = to;
        const short *sfrom = from;
        *sto++ = *sfrom++;
        to = sto;
        from = sfrom;
        n -= 2;
    }
    temp = n >> 2;
    if (temp) {
        long *lto = to;
        const long *lfrom = from;
#if defined(CONFIG_M68000) || defined(CONFIG_COLDFIRE)
        for (; temp; temp--)
            *lto++ = *lfrom++;
#else
        asm volatile (
            "    movel %2,%3\n"
            "    andw  #7,%3\n"
            "    lsrl  #3,%2\n"
            "    negw  %3\n"
            "    jmp   %%pc@(1f,%3:w:2)\n"
            "4:    movel %0@+,%1@+\n"
            "    movel %0@+,%1@+\n"
            "    movel %0@+,%1@+\n"
            "    movel %0@+,%1@+\n"
            "    movel %0@+,%1@+\n"
            "    movel %0@+,%1@+\n"
            "    movel %0@+,%1@+\n"
            "    movel %0@+,%1@+\n"
            "1:    dbra  %2,4b\n"
            "    clrw  %2\n"
            "    subql #1,%2\n"
            "    jpl   4b"
            : "=a" (lfrom), "=a" (lto), "=d" (temp), "=&d" (temp1)
            : "0" (lfrom), "1" (lto), "2" (temp));
#endif
        to = lto;
        from = lfrom;
    }
    if (n & 2) {
        short *sto = to;
        const short *sfrom = from;
        *sto++ = *sfrom++;
        to = sto;
        from = sfrom;
    }
    if (n & 1) {
        char *cto = to;
        const char *cfrom = from;
        *cto = *cfrom;
    }
    return xto;
}

strcpymemcpy的区别

1、复制的内容不同。strcpy只能复制字符串,而memcpy可以复制任意内容,例如字符数组、整型、结构体、类等.

2、复制的方法不同。strcpy不需要指定长度,它遇到被复制字符的串结束符"\0"才结束,所以容易溢出。memcpy则是根据其第3个参数决定复制的长度.

3、用途不同。通常在复制字符串时用strcpy,而需要复制其他类型数据时则一般用memcpy

用法及实例

/*
*本代码版权归高小调博客所有 
*作者:高小调
*日期:2016-8-13
*代码功能:各种情况下memcpy函数的用法
*集成开发环境:Microsoft Visual Studio 2010 
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(){
	char src1[]="Welcome to Gxd Blog!";
	char dest1[30]={0};
	int src2[]={0,1,2,3,4,5,6,7,8,9};
	int dest2[10]={0};
	int i = 0;
	//拷贝字符串
	memcpy(dest1,src1,sizeof(src1));
	//拷贝整型数组
	memcpy(dest2,src2,sizeof(src2));
	printf(dest1);
	printf("\n");
	for(i=0; i<sizeof(dest2)/sizeof(dest2[0]);i++){
		printf("%d ",dest2[i]);
	}
	printf("\n");
	system("pause");
	return 0;
}

输出

memcpy函数实例程序

小调祝你一切安好.

本文由高小调博客原创!

原文地址:http://gaoxiaodiao.com/p/24.html

如欲转载,请注明出处!

PS:热...我坚持在35度高温下工作,这么负责任的博主,中国也就仅此一家了.

(未完待续...)

上一篇:
下一篇: