• 面试:C/C++常见库函数实现


    1. void *mymemcpy(void *dest, const void* src, size_t n);

         内存拷贝函数,memcpy函数的功能是从源src所指的内存地址的起始位置开始拷贝n个字节到目标dest所指的内存地址的起始位置中

     1 void* memcpy(void* dest,void* src,size_t n){
     2     assert(dest != NULL && src != NULL);
     3 
     4     if(src < dest && (char*)src + n > dest){
     5         char* pdst = (char*)dest+n-1;
     6         char* psrc = (char*)src+n-1;
     7         while(n--){
     8             *pdst-- = *psrc--;
     9         }
    10     }else{
    11         char* pdest = (char*)dest;
    12         char* psrc = (char*)src;
    13         while(n--){
    14             *pdest++ = *psrc++;
    15         }
    16     }
    17     return dest;
    18 }

    2.void *memset(void *s, int c, size_t n);

    1 void* memset(void* src,int c,size_t n){
    2     assert(src != NULL);
    3     char* psrc = (char*)src;
    4     while(n--){
    5         *psrc++ = (char)c;
    6     }
    7     return src;
    8 }

    3. char* strcpy(char* dest,const char* src);

     1 char* strcpy(char* dest,const char* src){
     2     assert(dest != NULL && src != NULL);
     3 
     4     size_t n = strlen(src);
     5     if(src < dest && src + n > dest){
     6         char* pdst = dest + n;
     7         *pdst-- = '';
     8         src = src + n - 1;
     9         while(n--){
    10             *pdst-- = *src--;
    11         }
    12     }else{
    13         char* pdst = dest;
    14         while((*pdst++ = *src++) != 0);
    15     }
    16     return dest;
    17 }

    4. char* mystrncpy(char* dest,char* src,size_t n);

     1 char* strncpy(char* dest,const char* src,size_t n){
     2     assert(dest != NULL && src != NULL);
     3 
     4     if(src < dest && src + n > dest){
     5         int m = strlen(src) < n ? strlen(src):n;
     6         char* pdst = dest+m;
     7         *pdst-- = '';
     8         src = src + m-1;
     9         while(m--){
    10             *pdst-- = *src--;
    11         }
    12     }else{
    13         char* pdst = dest;
    14         size_t i = 0;
    15         while(i++ < n && (*pdst++ = *src++) != '');
    16         if(*(pdst-1) != '') *pdst = '';
    17     }
    18     return dest;
    19 }

    5.char* strcat(char* dest,const char* src)

    1 char* strcat(char* dest,const char* src){
    2     assert(dest != NULL && src != NULL);
    3     char* pdst = dest;
    4     while(*pdst != ''){
    5         pdst++;
    6     }
    7     while((*pdst++ = *src++) != '');
    8     return dest;
    9 }

    6.int strcmp(const char* s1,const char* s2)

    1 int strcmp(const char* s1,const char* s2){
    2     assert(s1 != NULL && s2 != NULL);
    3     while(s1 && s2 && *s1 == *s2){
    4         s1++;
    5         s2++;
    6     }
    7     return *s1 - *s2;
    8 }

    7.size_t strlen(const char* s)

    size_t strlen(const char* src){
        assert(src != NULL);
        size_t ret = 0;
        while(*src++ != ''){
            ret++;
        }
        return ret;
    }
  • 相关阅读:
    storm源码分析之topology提交过程
    storm启动supervisor源码分析-supervisor.clj
    storm启动nimbus源码分析-nimbus.clj
    storm shell命令源码分析-shell_submission.clj
    storm定时器timer源码分析-timer.clj
    Storm在zookeeper上的目录结构
    storm操作zookeeper源码分析-cluster.clj
    Four subspaces
    拉格朗日量(函数)、达朗贝尔原理、哈密顿量
    离散正弦信号的周期
  • 原文地址:https://www.cnblogs.com/wxquare/p/5014445.html
Copyright © 2020-2023  润新知