How to learn C++ for the purposes of debugging - c++

Some background: My job involves maintaining a large multi-threaded multi-process C++ / C# application, and so I'm often tasked with understanding access violations, memory leaks, heap curruption issues and the like.
I quite enjoy this, and I've amassed quite a good understanding of various low level concepts, but the trouble is that I don't program in C++, and aside for the purposes of maintenance I don't really intend to.
What I mean by that is that if I ever need to develop something then at the company I work at the best choice is C# (more developers, other apps also in C# means better interops), so its not that I don't program in C++, it's just that whenever I do program in C++ it will be purely for the purpose of learning C++, and so I want to get the most out of it.
My view is that "Teach yourself C++" books and the like aren't very suitable as they focus too much on getting things done - there are usually many ways of doing things and so they tend to pick one method, so when I'm presented with some code that does things a different way I'm stuffed (e.g. a book teaches MFC, I then get presented with some ATL code and the book hasn't even taught me what ATL and MFC are, let alone how to recognise that what I'm looking at is different!)
I'm really looking for teach yourself C++, with the emphasis on understanding other peoples code.

IMHO C++ in particular is a language that you cannot learn by reading a "teach yourself" book, you really need to have several sources and one of these is actually to look at other people's code.
I would recommend reading Effective C++ and More Effective C++ by Scott Meyers to learn some of the pitfalls when programming in C++, it is a good way to learn especially when you are looking at people's code. Another site than can help is gotw as well, some excellent information is there as well.

Since you'll never be creating C++ programs from scratch, I recommend you narrow your vision and just look at the applications you will be supporting, concentrating on the things you do not fully understand and tackling them one-by-one.
I find http://www.parashift.com/c++-faq-lite/ to be a good basic resource for C++. When dealing with specific technologies like MFC or ATL, do some research beforehand on which book(s) will suit you best. My favorite method: a strong coffee, a comfy chair and a pile of candidate books at Barnes & Noble to review. Focused on-line searching will suffice as well.
There are no shortcuts to knowledge and mastery, but by limiting your focus you can save yourself some wasted effort. Generally speaking, the more complex the technology--or the more fundamental the design screw up--the more you need to know to fix it. It's the facts of life!

The C++ FAQ is a great source of information.

Despite others' answers, I don't think your problems will lie mainly with the language. Sure, you can look at the standard or Stroustrup, but these will only teach you language constructs.
Most of what you will have trouble learning, I imagine, will be windows-specific and whateverplatformyouareusing-specific.
Do read stroustrup and other language guides, but be prepared to delve into the docs for the libraries and systems you are using.

It sounds like you really need a copy of the C++ standard. (ISO/IEC 14882 - available in draft form for free online. the final version costs a few bucks)
Of course, Stroustrup's book would be a good choice too. But in general, focus on material that describes the language, rather than, as you say, "how to get things done".

If you're having to maintain C++ code that uses MFC and/or ATL to a large degree then the best way to learn how it works so that you are able to properly maintain the code is to write small applications that use MFC and/or ATL so that you get an understanding of hwta is going on when the code is run. Reading a book by Stroustrup will not help you in any way to understand MFC and/or ATL. The other thing you should probably be focusing on is how COM works since ATL is basically a framework of templates, macros, etc that makes using COM easier.
Hope that helps.

Related

Resources for becoming more proficient with C++?

I am an undergraduate student and I recently was hired for a co-op for this summer and next fall. From what I understand I will be writing a lot of C++ which I am comfortable with but I don't have a huge amount of experience actually writing C++ in particular.
I wanted to take the time to really dive into C++ and become much more proficient with it. Does anyone have any particular resources that I could use to get some more experience before I start my job?
TopCoder was featured on This Developers Life recently and the algorithm challenges are a great way to hone your skills in various languages including C++
Getting involved in Open Source projects is a regular suggestion.
Good books from many authors are listed conveniently in many places including Amazon.com
To really deeply understand the language and learn some best practices I would recommend to read Herb Sutter's books, in following order:
C++ Coding Standards
Exceptional C++
More Exceptional C++
Exceptional C++ Style
The first one is very useful if you don't have experience with industrial development in C++.
Read The C++ Programming Language by Stroustrup cover to cover, and then read it again and again. Then, as you work with C++ programs, keep referring back to that book to remind yourself of the details of various topics.
It is, by far, the best book on C++ available, and since the author invented C++, you will get a good sense of the intentions and rationales of the various language features.
Once you have mastered the contents of that book, the next step is to familiarise yourself with the C++ 03 and the upcoming 0x standards. Reading a language standard can be hard, but once you get used to the style, standards are invaluable resources.
It is very important to be familiar with the authoritative resources and not get lead astray with possibly incorrect or out-dated information out on the web. I've seen programmers make really dumb mistakes because they just searched on Google to find out how some language feature works.
You need to practice and read manuals (tutorials) for c++.
Else you can try to look at internet website who are not protected and look at the source code.
Everybody work hard for what they learn and apply.
Good luck!

C to C++ : Transitioning from one language to the other [duplicate]

This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
C++ tutorial for experienced C programmer.
I program in a number of languages frequently and have been using C++ lately. Basically my classes are just wrappers around pure C code. Almost like a struct with associated methods. This gives me the encapsulation and privacy that I want for my data. I have a small hierarchy of classes and am just barely using inheritance.
I am familiar with OO concepts and know what search terms to use when i need to find out about a particular concept in this regard. However, as I have discovered in my foray in the programming world, often the language features that are really helpful are hidden to the newcomer or novice, and the useful bits that I need have already been written and are in a library somewhere that is freely available (most times part of the framework - like in .NET).
What path would you suggest to gain this vital knowledge in C++ and stop myself reinventing the wheel (poorly).
This is the wrong way to use C++. You would be better served grabbing a copy of Accelerated C++ and reading it. Yes, it's a beginner book but unless you want to continue treating C++ as just C with objects then you need to focus on how C++ programmers do things instead of just sticking with what you already know. You need to start from the beginning and build a good foundation in C++.
Learn the STL if you're going to really plan to use C++ in the future. Though opinions will vary widely, particularly among the die-hards, I think there is absolutely no problem using C++ as "C with objects."
Boost is also pretty awesome.
EDIT: Note the downvotes already coming in from the die-hards. C++ acolytes really don't like to hear people advocate using the language as "C with objects." I stand by my statement. You can write quite shippable and commercially viable code without going crazy with an RTTI-enabled, templatized, multiply-inherited set of classes. Remember KISS.
Scott Meyers' books are a great place for C programer to begin with C++.
I recommend Thinking In C++ by Bruce Eckle. Normally it's available for free online, or as a book.
I suggest you read the books:
"C++ Coding Standards: 101 Rules, Guidelines and Best Practices" --Sutter & Alexandrescu
"Modern C++ Design: Generic Programming and Design Patterns Applied" --Alexandrescu
And probably anything else by Andrei Alexandrescu that you can get your hands on.
Then, there are a number of design patterns and programming idioms that make it very clear why "C with objects" is extremely reductionist. Just to name a few: RAII (Resource Allocation Is Initialization), PImpl (or Cheshire Cat), Factory functions, Smart Pointers, Singleton, Type Traits, Expression Templates, etc. When you know about these, you are no longer programming in C++, but in ++C (because you get a result that actually reflects the increment over C).
As for not reinventing the wheel, like many have said already, make sure to first explore the possibilities in the Standard Template Library (STL) (which is much richer than you might think) and then look at Boost (www.boost.org) which has libraries for a lot of diverse purposes and they are extremely high quality (and some are just works of art, like Spirit, Proto, Lambda and MPL). After that, there is of course a large amount of open-source software in C++ out there, but use it with caution: sometimes it is better to reinvent a wheel that fits perfectly to your application than to use one that might not be appropriate or powerful enough, or worse, full of bugs!
I suggest the book The C++ Programming Language for filling in the gaps in your basic C++ knowledge, and BOOST as the first place to look for existing libraries supporting your programming.
Have you thought about getting the C++ Primer Plus? It's a really good book.
The C++ Standard Library: A Tutorial and Reference
Read Meyers for specific tips, but also Stroustrup's Design and Evolution. The latter gets into the motivation as to why C++ is what it is, and very much comes from a "how to improve C" viewpoint.
As for "The C++ Programming Language", 3rd edition is very long. If you can find the 2nd edition, its much more digestible, although of course occasionally out of date (but mostly just less complete).

Resources to learn C++ itself, not the basics of programming? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 9 years ago.
Improve this question
I'm something of a new, reluctant convert to C++ from a Pascal, Ruby, and PHP background. I haven't worked with Pascal since a few months of torture with "Delphi Turbo;" since then I've practically been eating and sleeping Ruby and PHP. I'm already well acquainted with object oriented programming and many various subjects.
My main trouble is that I already know the rudiments of C/C++ programming, less the actual experience of working with C++. I worked with C for long enough to realize that even without macros, etc, I didn't want to work with it. (Some of the disgust was from maintaining a CGI application in C, when accustomed to Pascal's automatic string management.) I know just enough C++ to be dangerous to myself and anyone else unwitting enough to use my "code."
I'd really like to work up to a good enough understanding of C++ to use libraries like Crypto++ and Boost without major problems or in-depth knowledge of the language's intricacies. I just need to figure out how to work with C++ without killing myself (either with C++, or with the long-standing dislike that I'm already battling).
What are effective resources that will teach me C++ without assuming that I must be retaught all basic programming? I'd rather not relearn concepts that I already know, unless the paradigm in C++ is significantly different. I'm also learning on my own time, so don't recommend me a book complex enough to need a guru to explain to me, please! And I have an effective budget of $0 for learning C++, so please keep suggestions to quality online resources or books common enough that I could likely find them at the library.
Read Effective C++ by Scott Meyers - it's a good guide for getting past the basics of C++ and showing how to write and use "correct" C++ code
The C++ FAQ Lite is a great resource.
I highly recommend Stroustrup: The C++ Programming Language (Third Edition). As the author of C++ he is the authority on the language, and the book is useful as a reference as much as it is for learning the language. It's common enough that most good general purpose libraries will have a copy. He goes into quite some depth on all the features of C++, including explanations of why certain design decisions were made in the language. Personally, I think this is the best book around for programmers to learn C++.
Once you have a good grip on core C++, David Abrahams and Aleksey Gurtovoy's book, C++ Template Metaprogramming, goes into further depth and provides many examples of how C++'s template system allows you to perform complex compile-time programming, a highly valuable skill these days. This one is a little less common but you can probably find it at a university library.
I can give you a couple of keywords you might want to research in more detail:
RAII (Is pretty much the technique that ensures you don't have to worry about memory leaks. Very convenient)
Generic Programming (The STL in particular. Experiment with iterators and the standard library algorithms, and see just how powerful these abstractions are. They're a key part of what I like about C++)
Functors (Perhaps too simple on their own, but the way they can be used instead of function pointers with the algorithms mentioned above is interesting)
And just get familiar with templates, and "mild" forms of template metaprogramming. (Traits classes, for example, and (partial) specializations.
And just keep an eye on the C++ questions here on SO. A lot of interesting topics are regularly brought up.
But the best advice is probably to keep it completely separate from C. Forget everything you learned about how to use C. It either doesn't apply in C++, or leads to inferior code that is harder to read and maintain.
It's an interesting language in its own right, and has a number of unique features. Leverage those, and it can actually be fun to work with C++. Treat it as an overengineered Java, PHP or C, and it'll just make you want to throw up.
You need to write code. A lot of code in C++. There is no substitute. You also need to read good code.
I agree with the suggestion for Scott Meyers' books though. Those are pretty good.
Part of your learning will be the leap from procedural programming to OO.
I would highly recommend the book "C++ Common Knowledge" by Stephen C. Dewhurst. Don't know if it's common enough to be found at the library (it's not at mine, but my library sucks for computer books that aren't 5-10 years out of date), but it does an excellent job of taking the complex aspects of C++ and making them easy to understand, without dumbing anything down for beginners. Definitely worth the investment.
To quote from the back of the book:
This book is for you if
You're no "dummy," and you need to get quickly up to speed in intermediate to advanced C++
You've had some experience in C++ programming, but reading intermediate and advanced C++ books is slow-going
You've had an introductory C++ course, but you've found that you still can't follow your colleagues when they're describing their C++ designs and code
You're an experienced C or Java programmer, but you don't yet have the experience to develop nuanced C++ code and designs
You're a C++ expert, and you're looking for an alternative to answering the same questions from your less-experienced colleagues over and over again
C++ Common Knowledge covers essential
but commonly misunderstood topics in
C++ programming and design while
filtering out needless complexity in
the discussion of each topic. What
remains is a clear distillation of the
essentials required for production C++
programming, presented in the author's
trademark incisive, engaging style.
Here is a link to a question with answers that should help you.
https://stackoverflow.com/questions/1227764/i-need-to-improve-my-c-skills-fast-is-this-realistically-possible/1227805#1227805
For effective stuff you can find online Cplusplus.com has a pretty good reference and information.
If you can find the book "C++ Common Knowledge" (Stephen Dewhurst) at the library or cheaply online, I would add that to the list posted on the StackOverflow link above as well as "The C++ Programming Language" (Stroustrup). Going through the questions under the C++ tag right here on SO should give you some good pointers and code examples to get you on your way.
Here's a list of good C++ books which teach you C++ rather than basics of programming.
Sit down and write some C++ code.

learning c++ from boost library source code

I am very interested in c++ and want to master this language. I have read a lot of books about c++. I want to read some library source code to improve my skill, but when I read the boost library source code, I find it is very difficulty.
Can anyone give me some advice about how to read boost source code and before I can understand it what kind of books about c++ should I read?
If you're starting out in C++, the boost source code is probably not the best place. It's where the wizards hang out and they deal in template magic. I think a better starting point are Scott Myers and Herb Sutters books (in that order).
Some versions of Scott's book can be a bit out dated but they are still strong in the fundamentals. Herb's books are worth reading many many times and are an invaluable tool. Once you've gotten through both of those authors, then would be a good time to tackle the boost source code.
Scott Myers: Effective C++
Scott Myers: Effective STL
Herb Sutter - Really I would go for anything with Effective and C++ in the title from this list.
I can't give advice on how to read boost code, but I can offer some other advice.
Stop reading and start writing :) Reading is valuable, but you won't learn anywhere near as much unless you start writing code yourself. Start with the basics. Read the beginners books and type out the samples (don't copy and paste). You'll learn by having to fix the errors that are the result of you mistyping. Play with some of your own ideas for simple applications and go from there.
Starting off by reading boost source code is a sure fire way of scaring yourself off the language and/or ending up very confused with a lot of questions.
Start small, work your way up.
Since you mention that you want to learn the dark art of meta programming then I would recommend "Modern C++ Design" by Andrei Alexandrescu.
Meta programming is a very complicated area and is not required most of the time. Once you learn about it, it is very easy to think that it can solve all your problems. It becomes your new favourite hammer.
I would also recommend becoming a very proficient user of libraries based on meta programming like boost and loki before you add it to your own code.
Two different programmers used meta programming in parts of the code base I am responsible for. While they were skilled programmers a commercial product should not be treated like a playground. These are probably the worst area's of our code base now, very complicated and very brittle esp when you add support for new compilers. If I was responsible for the code when they were written they would not be here, now they are too expensive to replace.
In short you very rarely need meta programming unless you are a library writer. And you cannot be a library writer without being a very accomplished library user.
I imagine boost uses a lot of advanced c++ features like templating etc. Boost libraries tend to be very complicated as they try to follow strict programming standards and styles for them to be compatible with things like the STL.
Quite simply, if you don't understand it, you are probably in over your head. and if you do understand it, you don't really need to be reading it anyway. Don't jump into the water if you don't know how to swim... You've essentially tried to swim up a waterfall.
Yeah, there is some truly awful stuff in some of the boost libraries. If you want to read some very nice source code, try checking out WebKit or some of the Google open source projects (like Chrome or Protobufs).
Id suggest you start off with something like Lipmann's Premiere(other begginers books of
your liking). Then, when you're comfortable with the code, you can move on to Herb Sutter,
Alexandresku to broaden your horizons, acquire a more complete view on the soft dev. And
MOST IMPORTANTLY - to be able to cede well one has to code A LOT, as it is a skill just as
any other and it takes practice to master.
P.S.> If you're confident you can pick up Stroustrup right from the start along with other
book(s). I don't think that theres another source on C++ just as complete as the authors.
For learning anything, i mean, there's a principle which is very important. To fill the belly we have to learn how to cook, to chase a girl we have to learn how to flirt with, to resist the beast we have to learn how to make fire. So finally we find that we learn and master a thing in the process of solving a specific problem.
In my perspective, it's best to learn and master the c++ language with diving into a specific problem waiting for solving. You analyse it, stick with it, try it, many times, with facing various difficulties, and solve it, finally, and bravo. With the delight of success you will find that the C++ language has been conquered, by you.

Learning c++ from a C# background

I want to learn c++ as I will be working on image recognition, etc.
I have a few years solid experience in C# and I have made stuff with C# so I'm not lacking experience. What is a good book which will help me make the transition (I will still be doing C# as it is my main skill)?
Also, would you agree that to be good at C++, a lot of experience and being proficient in C# will help? As C++ is harder...
Thanks
Also, would you agree that to be good
at C++, a lot of experience and being
proficient in C# will help? As C++ is
harder...
Yes I agree with that. A lot of experience in development in any language helps in my opinion. With experience comes appreciation of best practices. Those practices may be different, but you will not dismiss them outright because you know (from your experience) that they are usually good for you.
Good book... Get "C++ primer" with "Effective (and more effective) C++" and you will be all set. Then if you need STL get yourself Josuttis and "Effective STL". Good luck
Since you already know c# you already know more syntax than there is in c++. There are very few c++ syntax elements that you'll need that are not also available in C#.
Here are the main areas where I think you see some challenges:
a) Many, many constructs in c# are simply not available in c++. I'm talking about language features not data types. i.e. Generic Collections, etc... while similar results can be obtained with c++ language features it is soooo much more work in c++
b) It's really the libraries/framework you choose that are going to be substantially different.
c) Why not mix the two. They both play well together. Do your image processing work in a c++ library but keep the work there to a minimum and wrap the whole thing in c# for consumption in a UI. Of course the interoperability will be another challenge but it is well documented on the web.
As far as good book goes... there is still none better than Steve McConnell's "Code Complete"
http://cc2e.com/
Good Luck.
I've actually done the conversion the other way around. I wouldn't say C++ is hard. Only as hard as you can make it. There are certain standard to follow with such things as memory allocation, pointers, type casting and etc. But it's nothing you cannot iron out as you get deeper into it.
Actually, (and some may see this as an overkill) if i were you i would try to get a decent assembler book and read the first few chapters on registers, memory addresses, stack, heap and etc. I think it'll paint a better picture for you when you start messing with memory management, which is probably the hardest thing to grasp in C/C++.
I don't have any links on the subject, but I can offer some general advice.
Remember that you no longer have memory management. You have to delete your pointers after you're done with them.
In C++, there is no physical difference between a struct and a class. Both live on the heap or on the stack based on how you use them. In C#, a struct is a ValueType and lives on the stack, while a class is a ReferenceType and lives in the heap. In C++, a struct has public member visibility by default, while a class has private member visibility by default; that's it. In C++ a type (class or struct) lives on the stack by default, and only lives on the heap if you declare it as a pointer (and new it up).
Learn the Standard Template Library (STL). It's easily the best thing C++ has going for it.
Learn to hate the Microsoft Foundation CLass library (MFC), but learn to use it. If you're doing windows development in C++, you pretty much have to do it.
Also, would you agree that to be good at C++, a lot of experience and being proficient in C# will help? As C++ is harder.
Yeah I agree with you on the fact that C++ is harder. In fact it is considered to be one of the most complicated programming languages. Its syntax(at some places) is a bit ugly as compared to C# and Java but yeah it is one of the most widely used language in the industry, so best of luck with it.
As far as good books are concerned I'll go with
1) C++ Primer by Stan Lippman (strongly recommended)
2) Thinking in C++ by Bruce Eckel
and style books like Effective and More Effective C++ by Scott Mayers.
Apart from that the 'Bible' for C++ is "The C++ Programming Language by Stroustrup".
Enjoy!
I learned C++ in college and found the Dietel book extremely thorough.
Being proficient in C# helps but C++ is closer to C.