Sign in to follow this  
Padala Srikant

Where to find source code of inbuild library of c,c++

Recommended Posts

They are typically made available by the vendor of your compiler. In particular the bulk of the C++ standard library is header-only, due to the large number of templates, so you can simply open up those header files.

I have serious doubts you will learn anything of much value this way though. Reading source code without context is often a bad way to learn. The implementation choices made by the developers of those standard libraries were made with very different reasons than your own -- reasons you may not be fully capable of understanding, even, as a beginner.

I recommend you look elsewhere for guidance. What kinds of things are you expecting to learn here? Language syntax? Design of systems? What?

Share this post


Link to post
Share on other sites
Quote:
Original post by jpetrie
They are typically made available by the vendor of your compiler.

I am curious whether Microsoft did publish their codes?

Anyways, if after reading jpetries advice you still want, have a look at:
* GNU libc
* GNU libc++
* eglibc (the variant to which Debian switches)

Share this post


Link to post
Share on other sites
Quote:
Original post by phresnel
I am curious whether Microsoft did publish their codes?


The source code to the MSVC standard library are shipped with Visual Studio. I don't think they are with the express editions, but MSVS standard and above do come with the CRT source.

Share this post


Link to post
Share on other sites
Quote:
Original post by jpetrie
Reading source code without context is often a bad way to learn.


Not to derail the thread, but after reading that article I'm curious how beginners are supposed to go about learning. If we shouldn't look at source code and we shouldn't read tutorials what other mediums are there? Aside from school, obviously, which is hardly a fantastic place to learn about 3rd party libraries like OpenGL (at least in the case of my school). I mean, my school has some inexplicable obsession with teaching everything in java and only offers something like two entry level c/c++ classes. Should we buy books and try to learn from them, or are those crap too?

It seems that the importance of learning how to read source code is something that has been stressed to me more than once. I suppose that I'm kind of confused. The author of that article has such bad things to say about some of the biggest self-teaching methods, but doesn't point out a single alternative.

Share this post


Link to post
Share on other sites
Quote:

Not to derail the thread, but after reading that article I'm curious how beginners are supposed to go about learning

It's a valid topic.

Quote:

If we shouldn't look at source code

You shouldn't look at source code in isolation. As I explain early in the article:

Source code is devoid of context. It’s simply a miscellaneous block of instructions, often riddled with a fair bit of implicit assumptions about preconditions, postconditions, and where that code will fit in to the grand scheme of the original author’s project. Lacking that information, one can’t be sure that the code even does what the author wanted it to do! An experienced developer may be able to apply his insight and knowledge to the code and divine some utility from it (“code scavenging” is waxing in popularity and legitimacy, after all), but a beginner can’t do that.

"Code scavenging" in particular is a woefully increasingly common attempt at "solving problems" which typically does nothing to actually (a) solve a problem, (b) teach anybody anything. See examples given here.
Quote:

and we shouldn't read tutorials

You shouldn't read bad tutorials, just like you shouldn't read bad books or listen to people who don't know what they're doing. I have never said that "tutorials are bad," (or if I have, it was in a context where I was assuming the audience would understand the caveats that were to be placed on that generalization). I have observed many times that many tutorials are poorly written, a sentiment some fellow developers share. And I have specifically (in the blog post linked from the one you just read) argued that beginners should not be authoring tutorials. But tutorials can be a valid educational tool.

Source code can too, occasionally. "Don't Read Source Code" is a blog post that takes a very hard line; perhaps a more pragmatic thing to say would be that source code is probably among the least efficient educational resources since (unlike books, unlike tutorials) it is very rarely written to be educational, rather it is written to get something done (and thus lacks all that useful context I talk about, all that meat).

Quote:

Aside from school, obviously, which is hardly a fantastic place to learn about 3rd party libraries like OpenGL

OpenGL is just an API, learning how to learn an API is something a developer typically picks up over time. It just involves knowing the language in question, reading documentation, knowing how to search the web, et cetera. Learning the fundamentals of computer graphics is something many schools with decent CS degrees can teach you very well, however (although such classes are typically electives, not core classes).

Quote:

I mean, my school has some inexplicable obsession with teaching everything in java and only offers something like two entry level c/c++ classes.

Programming languages are tools; it is largely irrelevant which tools are taught to you, you will ultimately be able to understand how to apply many. Carpenters do not go to Hammer School. They do not go to Saw School or Axe School. They learn to build things, and learn that at a saw and an axe can both cut things up, but in very different ways... since they learned something higher level ('how to build things') they can extrapolate their knowledge of hammer, saw and axe more usefully than they could if they only new how to operate on (or a handful) of those tools in the small, in isolation.

The languages a school directly teaches in classes isn't usually a good metric for the quality of that school's education program.

Quote:

It seems that the importance of learning how to read source code is something that has been stressed to me more than once.

Learning how to read source code is very important, but this is a fundamentally different thing than reading source code to learn (in general).

Quote:

The author of that article has such bad things to say about some of the biggest self-teaching methods, but doesn't point out a single alternative.

I refer you to my above clarification regarding tutorials and the admission that the stance taken in that original blog post is perhaps too firm; I'm not trying to say that you should never read code, ever, I am saying that code generally the least-efficient method:

Pure source code offers no advantages and so many pitfalls that it is simply never a good choice.

...with the implication that any other methodology for self-learning you can think of is likely to be more efficient.

Share this post


Link to post
Share on other sites
Reading source code only helps if you have some semblance of what the original author was trying to do. Which is why just reading source code is a bad approach to learning how to program.

Now, if you wanted to learn how the stl vector works I would approach it like this:

- First attempt to write your own vector class. Design and implement it yourself and try your best to get it working.
- Once you have a working version or have failed to create a usable version, start looking at the stl version to see how they solved a particular problem.

Now that you've attempted a version yourself and seen how certain problems are solved in a standard version, you'll have a pretty good grasp on how a vector works without needing to know every last detail.

But, this approach will really only get you so far. IMO the best way to learn is to practice. Build a bunch of programs to do silly tasks for you. One of my first programs would calculate sales tax on a purchase for me. It was really trivial, but you'll take something away from everything you do.

Share this post


Link to post
Share on other sites
Learning to read code != learning from code.

Books are indeed a good source of information, although not all books are created equal so you are advised to ask for recommendations. For example learning about OpenGL is pretty much a case of 'pick up red book, read', learning about general 3d graphics is a case of 'pick up real time rendering, read'.

As are articles, which are not the same as tutorials, which explain concepts and maybe have some supporting code to illistrate the point. Again, not all things are created equal; most tutorials suck because they are code dumb + comments style of doing things.

The most important resource however is your own brain; having read books or decent articles the key thing is to try them out and given building blocks of knowledge consider, using your own logical thought, how you might go about doing other things. (imo this is a step many beginners miss, they expect too much hand fed to them instead of considering their own path).

At the risk of sounding like an old person; how do you think those of us who learnt to program at a young age pre-internet got on?
That's right; we got books, we read stuff and we tried things.

Share this post


Link to post
Share on other sites
I am asking that if I am asked to code a printf function which i think is not easy, than how can i achieve it without using any other inbuilt functions.......
I have no idea of how to do it.
Are there any books which can help me in this kind of stuff..

Share this post


Link to post
Share on other sites
On a modern OS you can't. You don't have direct access to the hardware necessary to do so with using some API function.

Share this post


Link to post
Share on other sites
I don't think MVC publishes all of source code. Last time I dug through there, many calls ended in DLLs, after going through horrible layers of C interfaces and ifdef jungle.

Share this post


Link to post
Share on other sites
Quote:
Original post by SiCrane
On a modern OS you can't. You don't have direct access to the hardware necessary to do so with using some API function.
Sure, but you can still implement printf() in terms of putchar() - I did so in assembly for an embedded system, a while back.

I wouldn't be surprised if printf() *was* implemented in terms of putchar(), in many implementations.

Share this post


Link to post
Share on other sites
Quote:
Original post by swiftcoder
Sure, but you can still implement printf() in terms of putchar()

Quote:
Original post by Padala Srikant
I am asking that if I am asked to code a printf function which i think is not easy, than how can i achieve it without using any other inbuilt functions....

Share this post


Link to post
Share on other sites
Quote:
Original post by Padala Srikant
I am asking that if I am asked to code a printf function which i think is not easy

Do you mean because of the variable number of arguments? Read this.

For the actual printing, my advice is to start small. Write a function that prints a number in decimal, using only putchar.
This can be surprisingly hard if you don't understand the difference between the bit pattern that is stored in an int (for example ..|...||), and the representation on the screen that consists of a sequence of characters between 0 and 9 (that would be 35 for the above example).
Getting this right for every possible input is nontrivial. Two numbers to watch out for are 0 and INT_MIN.

The next step is printing floating point numbers which is a lot more complicated.

The final step is writing a function that actually implements the parsing of the format string. I have never done that, because I think the type-safe solution C++ offers with the output operator acting on streams is far superior to printf. It would nevertheless be a valuable excercise to implement this, no doubt.

Share this post


Link to post
Share on other sites
Fair enough jpetrie. I wasn't necessarily trying to disagree with the article, it just sort of made me start wondering whether or no the things I've invested a considerable amount of time in have been teaching me bad habits, and how I'm supposed to really know, as a relative beginner to many things, when I'm reading something worth reading and when I'm reading something that is going to cause me problems in the long run.

Share this post


Link to post
Share on other sites
If you want to understand how the C++ Standard Library was created and how it is coded, read the following book:

Elements of Programming: Alexander Stepanov

Stepanov is the inventor of STL, so you know where this is coming from. This book is a very very difficult read, I would recommend being comfortable with some higher mathematics. Not because it's a pre-requisite for the material, but because exposure to some higher mathematics such as modern algebra ensures that you are familiar with the style of discourse and the level of rigor used in the book.

This book is interesting, in that IMO it's almost like a modern day version of Knuth's seminal work, only very specifically geared towards the STL and generic programming. On one hand it feels like you're reading a graduate mathematics book like Hungerford's Algebra, but on the other hand it feels like you're reading a book on C++ design techniques. The first two chapters seem to be going nowhere and you're not sure at first how it all ties in to the STL, but by the end of the book you can see how he came up with the motivation and design for STL.

It's not a book about syntax techniques, so it's not going to teach you template metaprogramming, or subtle C++ tricks like SFINAE. But it will teach you how to break down an algorithm, data structure, or concept into it's fundamental generic building blocks and abstract them in a way that maps directly to what can be expressed in C++. The rest is up to you.

All in all though, I highly recommend this book to anyone capable of digesting it.

Share this post


Link to post
Share on other sites
Quote:
Original post by Antheus
I don't think MVC publishes all of source code. Last time I dug through there, many calls ended in DLLs, after going through horrible layers of C interfaces and ifdef jungle.
Well, they don't publish the source code to Win32, and the CRT eventually calls into Win32 to do the actual work (i.e. malloc eventually calls VirtualAlloc, fopen eventually calls CreateFile, etc).

Share this post


Link to post
Share on other sites
He probably meant .obj file rather than DLL. There are quite a few core routines in Microsoft's CRT implementation that are written in assembly and only the object files for those are included. Though if you ever study the MSVC malloc() implementation you'll find that there are, depending on which version of MSVC you're dealing with, something like five or six different allocation methods malloc() can use. Which version ends up being called depends on an arcane mix of compiler options, host operating system and environment variables. IIRC, most of the time malloc() ends up calling RtlAllocateHeap() rather than any of the virtual memory functions directly.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this