实现自定义memcpy函数

来源:互联网 发布:数据标准该怎么制定 编辑:程序博客网 时间:2024/06/06 03:38

用char*指针实现memcpy函数!

// MyMemcpy.cpp : Defines the entry point for the console application.#include "stdafx.h"#include<iostream>/***********************************************************vs2015中测试通过,其他环境可能需要将_snprintf_s换成_snprintf************************************************************/void* MyMemcpy(void* dst, void* src, size_t len){    if (NULL == src || NULL == src || 0 == len)        return dst;    char* tmpsrc = (char*)src;    char* tmpdst = (char*)dst;    while (len-- > 0)    {        *tmpdst++ = *tmpsrc++;    }    return dst;}//测试对象Astruct A{    int nTest;    char szTest[50];    A()    {        nTest = 0;        memset(szTest, 0, sizeof(szTest));    }};int main(){    A a, b;    a.nTest = 1;    _snprintf_s(a.szTest, sizeof(a.szTest) - 1, "abcd f");    MyMemcpy(&b, &a, sizeof(A));    std::cout << b.nTest << std::endl << b.szTest << std::endl;    getchar();    return 0;}
0 0