Suggestions of excellent examples of real C/C++ code [closed] - c++

As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 10 years ago.
Locked. This question and its answers are locked because the question is off-topic but has historical significance. It is not currently accepting new answers or interactions.
I'd like to study some good C/C++ code. The code should:
be good in style and structure, presenting best practices
be a real life program (not an example or toy)
not too big so it doesn't takes ages to analyse it
Windows and/or Unix
I know there are 1000s of open source projects out there. But I'd like to hear your suggestions.
I am Java programmer and I am curious how good project in c/c++ is structured (files), granularity of classes/functions/files, documentation/comments, build tools etc.
Multiple suggestions are welcomed e.g. this is good windows example, that is good pure C etc.

I would specifically mention memcached. It's a great example of fairly short, readable code with a clear purpose.
Second, I would recommend the Apache web server. It's a fantastically well-run open source project that you'll learn a lot from, both about the language, as well as general design practices and networking/threading.

I'd vote for nginx: http://sysoev.ru/en/ as an example of a very good C programming style

Samba: a large, cross-platform (POSIX-based), pure C, very popular network server.
Samba has good developer documentation, and the code base itself is very well organized; I've had no problems diving into just about any location within the code to answer a question I had about its inner workings or to track down a bug.

I find the implementation of Lua is quite nice C source. But languages are a bit odd as an example project.

Diomidis Spinellis has some suggestions in his book, "Code Reading: The Open Source Perspective", Addison Wesley, 2003. ISBN 0-201-79940-5.
Perhaps the book could be useful to you.
URL http://www.spinellis.gr/codereading/

Google's Chromium is pretty huge, but you don't have to analyze it all to appreciate its clean, peer-reviewed structure.
Also, it's a good example of nicely done developer environment deployment.

Here's the main file for relayd, which is a daemon that handles load balancing and failover. It's nicely written, full featured, clean C code. It's big enough that you get a feel for a "real world" program, but not so big that you can't get your head around it with some work. Great for secure, well written networking code, daemons, etc.
Modesty aside, there's also a little tiling window manager for X that I've helped with. It's quite small but it's a real wm, and the other coders on it have high standards (and hold me to them!)... Nice for learning basic X stuff.

K&R has many good examples of real programs. For instance, on page 115 of the Second Edition there are two simple implementations of the echo command. From there, it might be interesting to see how the command was implemented over time. For instance, there is a version in the GNU coreutils source. A version derived from BSD can be found in BusyBox source.
It might be interesting to see how the newer versions compare to the textbook examples. Why are the programs that are used in real life longer? How much of the code is related to new features and how much is related to dealing with edge cases? How would you write the same code in Java?

As for C++ I would recommend you the Qt library sources and KDE's source code and its programs.
It's beautifully written code and the way that you should program in C++.

More C++ suggestions in: Examples of “modern c++” in action?

Related

Beginner game-development in C++ and lua [closed]

As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 10 years ago.
I am a total Beginner in C++ and programming generaly.
I have watched a couple of hours tutorals of C++ and read alot about it (Could you recommend some tutorials?), but what I´ve come across and couldn´t understand was Librarys.
I want to develop games in C++ (I think, it seems most interesting atm).
I can understand that I need a library for game programming in C++, but I don't know much about what a library is specifically, and where to find these librarys.
Also, I´ve read that Lua should be good to script the games with. I would like to know if this is true?
If its true, I would like to know where to find tutorials for Lua?
If not true, what is good then?
Thank you for your time.
This is my first question here.
I apologise if it wasn´t a proper question for this website, or if the english was bad.
I still hope you can give me some kind of answer and/or response.
You may want to try out Löve2d, a framework across Windows, Linux, and MacOS X for writing games in Lua. It bundles various game libraries for sound, graphics, physics etc so that you don't need to deal with that directly, and it lets you focus on the writing of games.
A good library to get started with game programming is SDL. http://lazyfoo.net/SDL_tutorials has some great tutorials to help you get started (the most comprehensive and easy to understand that I've found), but you need to understand program structure and such to get some practical use out of the tutorials.
OpenGL is pretty standard for 3D game programming, but it's more advanced than SDL. I'd recommend making sure you understand at least basic C++ before you move on to game programming... http://cplusplus.com/doc/tutorial would be a good place to start.
Here you can get very good and complete video tutorial of cs Indian institute of technology
I'm sure someone will come along and post an essay answering your questions in more detail, but here are the short answers.
A library is nothing more than a reusable collection of code. People write code and package it up for other people to use in their own projects. Libraries you would need for games include libraries that handle graphics, audio, networking, font/image loaders, etc.
Lua is great to script games with. It integrates very nicely with C/C++. Visit Lua's home site for tutorials.
Check this link out http://cplusplus.com/doc/tutorial/ for libraries here is the STL http://cplusplus.com/reference/.
Here is a reference on Lua, http://lua.gts-stolberg.de/en/index.php?uml=1.
can understand that much that I need a library for game programmig in C++, but I dont know much about, what A library is specificly, and where to find these librarys.
#TheBuzzSaw already answers what a library is, so I'll answer where to find. But wait, before WHERE, you have to know WHAT library to use. There are a lot of game libraries out there, both low level (loosely coupled between components) and high level (tightly coupled). The low level ones have separate functionality for graphics, sound, AI, etc. processing and usually each functionality can be used for things other than games. While the high level ones "glue" them together to form something that's ready to use as a whole (for instance, it provides scene class with events, music, etc. you just have to set some values to use). Due to the many number of then, I can't really say where to find before you know which one to use. One quite complete one is SDL, it's a low level one. Allegro is another one.
Also, I´ve read that Lua should be good to script the games with. I would like to know if this is true? If its true, I would like to know where to find tutorials for Lua? If not true, what is good then?
Lua is good, and it's used a lot in games. Ruby is good, some games use it. Python is good as well. Basically, most scripting languages are good for game scripting. You just have to consider what language features your game required, how fast you want it to be usually contradict with its syntax flexibility, etc.

How to develop small software or application? [closed]

As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 11 years ago.
Hya Gurus,
I've been learning c++ for 8 - 10 months and want to develop a software or small applications (i am self taught using some books) so just consider me as C++ entry level programmer.since i don't know win32 etc for GUI development i can use some library.
I know most of c++ features , data structs , algos and have read some c++ book and scott meyer's effective c++.
but problem is that i am unable integrate all my knowledge to build a software and i think i am a adhoc coder. when i see even small simulation or little application codes i understand the code sometimes (sometimes not since understanding them is hard by just seeing code) but i find even code of page 4 - 7 so large that i think i can't develop applications.
for e.g : if i want to develop my own chm reader or FTP or any appz or softw you consider serious coding, how do i develop it? then i find myself completely lost by just start typing code i know won't ever be able to develop it. (i feel 0% confident)
so what i want to know how programmers like you guyz learn to develop serious application when after drilling their teeths in c++ only, do i need some other books to learn how to develop software? or what is the process to become serious application developer ?
Any help is very appreciated.
P.S (Impotant) : i'd love if you could tell me how you became serious developer after learning C++ , what you had done etc pleaseeeeeeeeee. and any personal advices to me .
Edit:
I don't know UML or Soft Engineering , do i need to learn them?
Thanks a lot again ))
Programming large applications is a long and arduous process. Even with medium sized software.
Even the veterans need time to read new code. I remember just recently starting work on an existing software project that had around 150 classes. It took a while just to figure out how the basic input was handled, let alone the whole structure.
What you need to focus on is increase your confidence in building software.
Try creating something trivial, something you know you can program. Try to make it a bit bigger by adding some features, not much though, and keep doing that. Over time you will be a bit more confident in big code.
Programming takes practice and you need to keep at it. It might sound like a daunting undertaking but real confidence in a language takes years of practicing.
Look at the questions here about good software books as well.
I would suggest that you read up on the usenet groups on c and the books by Scott Meyers and Andrei Alexandrescu. Whey don't you try to find out what the Loki lib can do.
Also a good place to start is looking at the boost libs.
Find some small project that you like and see what you can learn, it is an iterative process.
You can also join an open source project.
Good luck,
Lars
you must have perform practicals every day and start with small application then watch project code i hope by performing practical you will easy to understand any code.

Why doesn’t WPF support C++.NET - the way WinForms does? [closed]

As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 10 years ago.
As a C++ stickler, this has really been bugging me. I've always liked the idea of the "language-independant framework" that Microsoft came up with roughly a decade ago. Why have they dropped the ball on this idea? Does anyone know the reasoning behind it?
Part of the reason will be that C++ support is actually two languages in one -- the native and the CLI variants; that extra development load has been acknowledged by the Visual C++ team as the reason that proper MSBuild integration lagged (lags? I haven't checked in 2008 or later) behind other languages.
Another part will be to do with the code generation during compilation that goes on in a C# build to support e.g. the binding "magic"; I've found that even in F#, you don't get it "just happening".
If it were me my reasoning would be that C++.Net should not be used to write GUIs.
I'm not trying to be snarky here, maybe someone can show me the error of my ways but I don't think it's a good idea. I'm messing around with one right now and development much much slower than if the application had been written in C#. My feeling is if features in C++.Net or just regular C++ are required for the application it seems like a better idea would be to create a DLL to do the heavy lifting and could interface with C#.
It bugs me too, if they had supported it, we'd be able to migrate our C++ code to a new GUI much easier and cheaper than basically rewriting everything in C#. Its costing us a fortune to rework our apps, just what we wanted in a recession.
I imagine the reasoning is that C# is popular (and not as cross platform as C++) so they've decided to keep their development efforts to the minimum required.
You can do WPF with managed C++.
The reason is that nearly all new application programming is now done in JavaScript, Java, VB.NET or C# - all GC languages. The emphasis is on higher quality for a lower skill set and C++ demands too much from the developer, companies want people to write log bug code on their first day.
C++ for applications is mainly for maintenance of existing applications or where extreme performance is needed. Device drivers and OS are still frequently written in C++ but even that is changing (Coyotos is Cbit , E# , Cosmos/Mosa are C#, Singularity/Midori).

Well written C++ examples [closed]

As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 11 years ago.
I'm currently learning C++ and would like to start reading others sourcecode to pick up tips. I was wondering if anyone has examples of well written C++ that I can take a look at (and not pick up bad habits from)
As you've probably discovered, there are many Internet sources for examples of C++ code. StackOverflow, CodeProject, etc. all have examples of source. But of course, they're all over the map in terms of quality and educational value.
I think, in this area, books still trump the Internet. There's no substitute for going to the bookstore, thumbing through a few tomes, and picking one that you find readable. They've been proof-read (unlike may Internet submissions), so the samples are more likely to work and to be of high-quality.
While the books typically have small snippets of code or a few functions, they usually come with a CD or URL that points you to more full-blown examples.
O'Reilly, Thinking in C++, Petzold, and Wrox (and more) all have good books on C++.
The Boost library? It is generally considered some of the highest quality C++ code written. (A lot of it is also more or less unreadable unless you're a C++ guru yourself, though)
In general, I'd advise against you to be cautious with this approach though. In C++, the source code probably won't be as informative as in many other languages.
If you see some Python code, you can pretty much assume it's correct as long as it runs. If it's written by someone who seems to know what they're doing, you can even assume that it's well written.
In C++, there are just so many nasty pitfalls and subtle exceptions to every rule that you really need to know what you're doing. Going by what compiles, or what seems to work, or what you saw in someone else's source code is dangerous, and pretty much guarantees that you'll sooner or later end up with a program that relies on undefined behavior, and will crash when you least expect it.
If I were you, I'd try to stick to books. There are some very well written ones, which in addition to letting you see some source code, also teach you the langage "properly". And as long as you stick to reputable authors, you're ensured that they won't teach you any bad habits or plunge you into nonportable code.
In my opinion if you read a good C++ book (like "The C++ Programming Language", "(More) Effective C++" or "Exceptional C++").
You will not only learn good practices but should also get sense of how to write code.
Of course the samples in these Books are mostly artifical. If you read 'real-world-applications' you will always encounter pieces of code which are pretty ugly, but sometimes there wasn't just a nice clean solution for it (or a not-so-clean solution was just more efficient in terms of speed).
So I don't know if it's best to start with real applications since they can also be very overwhelming due to the amount of codesize and complexity, whereas sample codes in Books are compact and clearly laid-out.
I think for starters you would be best off reading such references as the books I have listed.
If you have to be flexible at some point in the future and have to produce ugly code, you will at least know that it's ugly code and not mistake it for "that's how it should be" ;)
Reading through open source software can be very educational if you already have a little bit of knowledge of C++. If you're just starting out, I imagine the Boost libraries will go right over your head. You could start with the WebKit project, which is an excellent resource. Also Google releases a lot of their source code, which also happens to be VERY clean: Protocol Buffers is a great example. And while you're at it, you may as well read their C++ Style Guide as well.
Personally, I started learning C++ by picking up a couple of books and writing some little challenge applications. Bear in mind that learning a language as extensive as C++ takes a long time, like 10 years long.
Many years ago, I was told that if I ever wanted a career as a C++ developer, I should definitely read Effective C++ by Scott Meyers. That should prevent you from falling into many of the language's pitfalls. If you find you need something even simpler, start with The C Programming Language by Kernighan & Ritchie. Be patient and good luck!
If you want a complete project to browse through, I would recommend Ogre3d. It is a well structured graphics engine with decent documentation and the source code itself is nice, too.
How about: Programming: Principles and Practice Using C++ by Bjarne Stroustrup?

What is the best encryption library in C/C++? [closed]

As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 11 years ago.
What is the best encryption library in C/C++ In terms of:
entropy
quality
ease of use
readability
portability
performance
What's your favorite and why do you like it?
We've used OpenSSL with good success. Portable, standards compliant and easy to use.
I've used CryptoPP in the past (http://www.cryptopp.com/) and although its API style and programming paradigms take a little getting used to, I liked it in the end. It provides a wide range of symmetric and asymmetric algorithms with much flexibility. Documentation is so-so, the API docs are there but there's little 'high-level' overview and simple sample code. I ended up puzzling together pieces of code from around the net. It was easy to integrate into my project (linked statically). I'm using MSVC - 2003 when I started using it, now switched to 2008. It's portable across several platforms (check out the website). I've always used the default RNG, don't know which one that is.
I'm gonna have to go with LibTomCrypt. It's often overlooked for OpenSSL, but TomCrypt is just so lightweight and simple. As for quality, TomCrypt is widely accepted as top-quality encryption. Also, it's license is public domain which avoids the attribution hassle for your documentation that BSD licenses give you when writing commercial software.
Crypto++ seems to have a very good reputation
Wikipedia - https://en.wikipedia.org/wiki/Crypto%2B%2B
GitHub - https://github.com/weidai11/cryptopp
My favorite is GNU's library:
libgcrypt
Its performance is good, and it's used EVERYWHERE so it's very well tested.
The C++ version isn't out yet but goolge KeyCzar http://code.google.com/p/keyczar/ might be worth looking at. Whatever you feel about their business they do have a lot of smart programmers working for them.
GPGme. Simple to use and compatible with the OpenPGP format