Source code for c / C ++ functions

I wanted to look at the implementation of various C / C ++ functions (e.g. strcpy, stcmp, strstr). This will help me learn good coding techniques in c / C ++. Could you tell me where I can find him?

Thank.

+16
c ++ c
Jul 14 '09 at 18:59
source share
14 answers

You can check out a glibc copy that will have the source code for all the C functions in the C standard library. This should be a good starting place.

+25
Jul 14 '09 at 19:02
source share
— -

It is worth noting that the source code for the C standard library does not necessarily highlight good coding methods. As a standard library, it has a special status, where, for example, it can rely on many unspecified or non-portable actions simply because they know the exact compiler with which it is used.

And, of course, this only talks about C encoding methods.

This has nothing to do with C ++ coding practice, which is very different. Do not treat them as one language. There is no such thing as C / C ++. C is a language with its own coding methods and common idioms, and C ++ is a separate independent language that 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.

+14
Jul 14 '09 at 19:17
source share

Many of the standard C library functions have source lists and discussions in the C programming language from Kernighan and Ritchie . Discussions are a useful way to learn more about the specifics of the C language and how functions in the standard library work under the hood.

+6
Jul 14 '09 at 19:04
source share

Most compilers provide source code for the library — however, this source code is usually more complex and confusing than you might expect.

A good resource for this is the PJ Plauger book, "The Standard C Library" , which can be pretty cheap if you go for a used one. The code presented is not always straightforward, but Plauger explains it pretty well (and gives reasons why it may not always be straightforward and still follow the standard).

+5
Jul 14 '09 at 19:13
source share

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

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

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

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

You can also check out uLibc, which is likely to be simpler than the GNU library:

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

To give you a taste, here is 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

+4
Jul 14 '09 at 19:06
source share
+2
Jul 14 '09 at 19:02
source share

"Standarc C Library" is a specification. You need sources to implement the specification. Your C compiler may or may not provide such sources - one that does GCC .

+2
Jul 14 '09 at 19:08
source share

You can also view the source OpenBSD tree . In particular, you want a subdirectory of the libc line .

+2
Jul 14 '09 at 19:08
source share

About 10 years ago I read the Plauger Standard C Library . Thoroughly recommend it.

+2
Jul 14 '09 at 19:16
source share

Here you go

The implementation will be slightly different from OS to OS, but the GNU / Linux implementation is likely to be the easiest to find there.

+1
Jul 14 '09 at 19:05
source share

Here are the common strXXX C functions in NetBSD: http://cvsweb.netbsd.org/bsdweb.cgi/src/common/lib/libc/string/

Here's the strXXX NetBSD implementation for i386 processors http://cvsweb.netbsd.org/bsdweb.cgi/src/common/lib/libc/arch/i386/string/

0
Jul 14 '09 at 19:10
source share

If you are using Visual Studio Professional or Team, you can find the source here:

C: \ Program Files \ Microsoft Visual Studio 9.0 \ VC \ crt \ src

0
Jul 14 '09 at 19:12
source share

For your purposes, this book may be useful: Master Algorithms with C

0
Jul 14 '09 at 19:14
source share

In general, I find BSD libc much easier to read than GNU. There is less "gcc-isms", the kernel is much clearer in intentions, etc. For example, the BSD code for malloc is quite readable compared to glibc

0
Jul 15 '09 at 0:37
source share



All Articles