⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mem.cpp

📁 常见算法的代码包
💻 CPP
字号:
/*******************************************************************************
*memcpy - Copy source buffer to destination buffer
*
*Purpose:
*       memcpy() copies a source memory buffer to a destination memory buffer.
*       This routine does NOT recognize overlapping buffers, and thus can lead
*       to propogation.
*
*       For cases where propogation must be avoided, memmove() must be used.
*
*Entry:
*       void *dst = pointer to destination buffer
*       const void *src = pointer to source buffer
*       size_t count = number of bytes to copy
*
*Exit:
*       Returns a pointer to the destination buffer
*
*Exceptions:
*******************************************************************************/

void * memcpy(void *dst, const void *src, unsigned int count) 
{ 
    char *pbTo = (char *)dst; 
    char *pbFrom = (char *)src; 
    assert(dst!= NULL && src != NULL); 
    while (count--) 
    { 
        *pbTo++ = *pbFrom++; 
    } 
    return dst; 
}

/************************************************************************
*Purpose:
*       memmove() copies a source memory buffer to a destination buffer.
*       Overlapping buffers are treated specially, to avoid propogation.
*************************************************************************/

void * memmove(void *dst, const void *src, unsigned int count) 
{ 
    char *pbTo = (char *)dst; 
    char *pbFrom = (char *)src; 
    assert(dst != NULL && src != NULL); 
    if (dst <= src || pbTo >= pbFrom + count)  //没有overlap的情况,直接拷贝 
    { 
        while (count--) 
        { 
            *pbTo++ = *pbFrom++; 
        } 
    } 
    else 
    { 
        pbTo = pbTo + count - 1;               //overlap的情况,从高位地址向低位拷贝 
        pbFrom = pbFrom + count - 1; 
        while (count--) 
        { 
            *pbTo-- = *pbFrom--; 
        } 
    } 
    return dst; 
}

/******************************************************************************
*memset.c - set a section of memory to all one byte
*
*       Copyright (c) Microsoft Corporation. All rights reserved.
*
*Purpose:
*       contains the memset() routine
*
*******************************************************************************/

void * memset(void *dst, int val, unsigned int count) 
{ 
    char *pvTo = (char *)dst; 
    assert(dst != NULL); 
    while (count--) 
    { 
        *pvTo++ = (char)val; 
    } 
    return dst; 
}

/*******************************************************************************

*Purpose:
*       Searches at buf for the given character, stopping when chr is
*       first found or cnt bytes have been searched through.
*******************************************************************************/

void * memchr(const void * buf, int chr, unsigned int cnt)
{
        while ( cnt && (*(unsigned char *)buf != (unsigned char)chr) ) 
		{
                buf = (unsigned char *)buf + 1;
                cnt--;
        }
        return(cnt ? (void *)buf : NULL);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -