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
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
It's worth pointing out that the source code for the C standard library does not necessarily highlight good coding practices. As the standard library, it has a special status where, for example, it can rely on a lot of unspecified or non-portable behavior simply because they know the exact compiler with which it is used.
And of course, this only tells you about C coding practices.
That has nothing to do with C++ coding practices, which are very different. Don't treat them as one language. There is no such thing as C/C++. C is a language with its own coding practices and common idioms, and C++ is a separate, independent language which also has its own practices and idioms.
Good C code is rarely good C++ code. strcpy
and other C library functions are certainly not good C++ code.
You could check out a copy of glibc, which will have the source code for all C functions in the C standard library. That should be a good starting place.
Here you go
The implementation will vary somewhat from OS to OS, but the GNU/Linux implementation is probably going to be the easiest one to find out there.
Most compilers provide source code for the library - however that source code is usually rather more complex and convoluted than you might expect.
A good resource for this is P.J. Plauger's book, "The Standard C Library", which can be had pretty cheaply if you go for a used one. The code presented is not always straight-forward, but Plauger explains it quite well (and gives the reasons why it can't always be straight-forward and still follow the standard).
About 10 years ago, I read The Standard C Library by Plauger. Thoroughly recommend it.