Reputation:
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 you let me know where can I find it?
Thanks.
Upvotes: 36
Views: 35420
Reputation: 340426
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).
Upvotes: 8
Reputation: 80770
In general, I find the BSD libc much easier to read than the GNU one. There are less "gcc-isms", the core is much clearer in intent, etc... For example, the BSD code for malloc is quite readable compared to glibc
Upvotes: 0
Reputation: 248209
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.
Upvotes: 18
Reputation: 118156
About 10 years ago, I read The Standard C Library by Plauger. Thoroughly recommend it.
Upvotes: 2
Reputation: 25429
For your purposes, this book may be helpful: Mastering Algorithms with C
Upvotes: 0
Reputation: 4425
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;
}
Upvotes: 5
Reputation: 74290
If you use Visual Studio Professional or Team, you should be able to find the source here:
C:\Program Files\Microsoft Visual Studio 9.0\VC\crt\src
Upvotes: 0
Reputation: 229274
Here's the general strXXX C functions in NetBSD : http://cvsweb.netbsd.org/bsdweb.cgi/src/common/lib/libc/string/
Here's the NetBSD strXXX implementation for i386 processors http://cvsweb.netbsd.org/bsdweb.cgi/src/common/lib/libc/arch/i386/string/
Upvotes: 0
Reputation: 784
You could also have a look at the OpenBSD source tree. Specifically, you want the string subdirectory of libc.
Upvotes: 2
Reputation:
"The Standarc C Library" is a specification. You want the sources for an implementation of the specification. Your C compiler may or not provide such sources - one that does is GCC.
Upvotes: 2
Reputation: 60529
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.
Upvotes: 1
Reputation: 10680
Many C standard library functions have source code listings & discussions in The C Programming Language by Kernighan & Ritchie. The discussions are a helpful way of learning more about the specifics of the C language & how functions in the standard library work under the hood.
Upvotes: 6