【C语言】内存操作函数的实现

内存操作函数

  • memcpy
  • memmove
  • memset
  • memcmp
  • memchr

模拟实现上述函数(编译环境VS2013)

memcpy

函数原型void * memcpy ( void * destination, const void * source, size_t num );

#include 
#include 
#include 

void * my_memcpy(void * det , void * src, int count)
{
    void  *ret = det;
    while (count--)
    {
        *(char*)det = *(char*)src;
        det = (char*)det + 1;
        src = (char*)src + 1;
    }
        return ret;
}


int main()
{
    char str1[20];
    char str2[] = "hello wsbg";
    char *ch = (char*)my_memcpy(str1, str2, strlen(str2)+1);
    printf("%s\n", ch);
    system("pause");
    return 0;
}

memmove

函数原型void * memmove ( void * destination, const void * source, size_t num );

#include 
#include 
#include 

void * my_memmove(void * dst, const void* src, int count)
{
    char* dst_s;
    char* src_s;
    dst_s = (char*)dst;
    src_s = (char*)src;
    if (dst_s > src_s && (src_s + count) > dst_s)   //第二种覆盖情况
    {
        dst_s = dst_s + count - 1;
        src_s = src_s + count - 1;
        while (count--)
        {
            *dst_s-- = *src_s--;
        }
    }
    else  //第一种覆盖情况
    {
        while (count--)
        {
            *dst_s++ = *src_s++;
        }
    }
    return dst;
}

int main()
{
    char str[] = "memmove can be very useful......";
    my_memmove(str+20,str+15,11);
    printf("%s\n", str);
    system("pause");
    return 0;
}

内存覆盖的两种情况图示
【C语言】内存操作函数的实现_第1张图片
memcpy与memmove的区别
当需要拷贝的原内存空间与目的内存空间有重叠时,memcpy不能保证拷贝结果的正确,但是memmove可以保证重叠时结果正确。

memset

函数数原型
void * memset ( void * ptr, int value, size_t num );

#include 
#include 
#include 

void * my_memset(void *str, int value, size_t num)
{
    char ch = (char)value;
    char * str_s = (char*)str;
    while (num--)
    {
        *str_s++ = ch;
    }
    return str;
}

int main()
{
    char str[] = "memset can be very useful......";
    my_memset(str,'+',5);
    printf("%s\n", str);
    system("pause");
    return 0;
}

memcmp

函数原型int memcmp ( const void * ptr1, const void * ptr2, size_t num );

#include 
#include 
#include 

int my_memcmp(const void * str1, const void * str2, size_t num)
{
    char * str1_s = (char*)str1;
    char * str2_s = (char*)str2;
    while (num--)
    {
        if (*str1_s > *str2_s)
            return 1;
        else if (*str1_s < *str2_s)
            return -1;
    }
    return  0;
}


int main()
{
    char str1[] = "123abc456";
    char str2[] = "123bbc456";
    int ret = my_memcmp(str1, str2, 3);
    printf("%d\n", ret);
    system("pause");
    return 0;
}

memchr

函数原型void * memchr ( void * ptr, int value, size_t num );

#include 
#include 
#include 

void * my_memchr(void * str, int value, size_t num)
{
    char * str_s = (char*)str;
    char ch = (char)value;
    while (num--)
    {
        if (*str_s == ch)
            return (void*)str_s;
        str_s++;
    }
    return NULL;
}

int main()
{
    char str[] = "123bbc456";
    char *ch = (char*)my_memchr(str,'2', 7);
    printf("%s\n", ch);
    system("pause");
    return 0;
}

你可能感兴趣的:(c语言)