source code of c/c++ functions

前端 未结 14 2257
抹茶落季
抹茶落季 2020-11-28 04:33

I wanted to have a look at the implementation of different C/C++ functions (like strcpy, stcmp, strstr). This will help me in knowing good coding practices in c/c++. Could y

14条回答
  •  孤街浪徒
    2020-11-28 05:12

    Look at an implementation of the libc standard C library. To see how a real, popular C library is implemented, try looking at the glibc code. You can access the code using git:

    git clone git://sourceware.org/git/glibc.git

    As for C++, you can find the glibc++ standard library on one of these mirrors:

    http://gcc.gnu.org/mirrors.html

    You can also check out uLibc, which will probably be simpler than the GNU library:

    http://git.uclibc.org/uClibc/tree/

    To give you a flavour, here's the strncpy implementation from uLibc:

    Wchar *Wstrcpy(Wchar * __restrict s1, const Wchar * __restrict s2)
    {
        register Wchar *s = s1;
    
    #ifdef __BCC__
        do {
            *s = *s2++;
        } while (*s++ != 0);
    #else
        while ( (*s++ = *s2++) != 0 );
    #endif
    
        return s1;
    }
    

    Here is strcmp and strstr

提交回复
热议问题