How to learn to program C ++ correctly

I did a lot of C / C ++ programming for my academic courses and was under the impression that I had a pretty good understanding of this. but lately I had to work in a bluetooth application that had a server and client implementation in a Linux box and embedded system. I learned about Bluez bluetooth API, socket / network programming and coded it.

However, I ran into a lot of problems with memory leaks and segmentation and other memory related bugs as the code got more complex, but everything lost control over pointers and streams and sockets. it got me thinking that I had a lot to learn that they didn't say in the main C / C ++ books. so I wanted to request available resources to help professional code in C / C ++, especially for Linux / Mac environment (gcc compiler).

Edit: changed from C to C ++ due to the confusion it was creating.

+2


a source to share


10 replies


This question is too big, too big.

In short, there is little you can do other than keep going. You will receive segfaults hits many more times. The only thing you can do is keep focus, hunt for these bugs relentlessly, and always trust the bug in your code and not in the compiler or some solid library you are using. When you get stuck, write specific, narrow questions here with appropriate code. Then we will help you.



Now, from here it really depends on which language you are using: C or C ++? Such different words are not enough. If this is C ++ then your first advice I will give is using RAII all the time . If you are using C, then always be aware of what owns that pointer, when and where it frees it, and most importantly, where the pointer points to. Also, always initialize your data, especially pointers. Never mind performance before its time, except for great performance.

What is it. Beyond that, post your specific issues and we'll look at them. What needs to be learned correctly.

+5


a source


If you malloc

forgot free

you have a memory leak, what else do you need to know to avoid them? It's all about design patterns, they are pretty linguistic agnostic though ...

Segfaults is another story, of course ... If you really want to know how to code C properly, skip to the copies of the C99 standard . I cannot be partially sure what I am writing in C unless I know exactly what the standard says about it. Likewise for C ++.



EDIT If you are just a beginner in C or C ++ you can read a book about them first, otherwise the standards will probably be hard to understand.

+4


a source


My starting tip for solving problems with memory and correctness of your code starts not with the language, but with programming practice in general:

  • Program a bit at a time, then check.
  • Test, test, test! Unit test where possible. Catch mistakes early and gradually.

I've made sure that C and C ++ will surely punish you the longer you keep testing your code. I bet you've tried to debug enough of these problems to know that finding a problem in an untested, non-trivial codebase can be confusing and painful.

For books to help you develop the discipline in this direction, I recommend the Pragmatic Programmer , Andrew Hunt and David Thomas, Addison-Wesley Professional, 1999 as a starting point .

The tips I have for C and C ++:

  • Don't dynamically allocate if you don't need to.
  • Watch dynamic memory allocation (malloc / new) like a hawk. Make sure you define exactly who owns the objects and is responsible for deleting them as soon as they are created - even in case of exceptions! (Which is where the resource initialization happens.) Build your code so that this right is logical and predictable.
  • Use Valgrind, Purify, and other similar tools to help catch and diagnose memory problems.

Two Books on Stack Overflow The Ultimate Guide and List of C ++ Books (great list by the way!) That I think will help you the most with this:

+3


a source


I can't comment on C, but with C ++, getting a good book the definitive guide to the book and starting small projects is a great start to learning the right coding techniques.

+1


a source


It can be seen from your comment that you think memory management is the same in C and C ++. This is actually a completely different story.

Thanks exceptions

to C ++, you get a new and better way to manage error checking and resource management. The standard practice is called RAII (Resource Initialization) .


First, read this thread: whats-the-difference-between-c-and-c

And then some great books: the-definitive-c-book-guide-and-list

+1


a source


Many of them will study experience. You can read and read, but many times you just need to dive in. The only thing I will say is to use a symbolic debugger. Setting breakpoints and pinpointing all your variables will speed up finding problems and fixing them by 10 times.

+1


a source


I think @James McNellis makes the best point. Each language has its own strengths and weaknesses. Good programming uses these aspects of a particular tool to get the job done in the most stable and optimized way you can use. Only after writing many programs will you be able to foresee when is the best time to use a particular methodology, and this is the programming wisdom you want and seek. So keep writing programs and keep trying to make them better.

0


a source


To manage resources and prevent memory leaks in C ++, you need the RAII pattern and smart pointers. Browsing the Marshall Cline C ++ FAQ Lite is also invaluable for learning the nuances of the language.

0


a source


I think it is important to recognize and accept the following early on:

0.) C ++ is not a superset of C, C and C ++ - they are two separate languages ​​with different differences.

1.) C and C ++ are powerful languages, but they are not particularly friendly to the novice programmer.

2.) Successful C / C ++ programmers never stop learning. Learning new material and reviewing what you've already learned is essential for programming in C or C ++.

3.) Programming in C or C ++ requires not only knowledge of the language, but also knowledge of the concepts underlying the language, tools that are commonly used to develop in the language (debuggers, link collectors, compilers, libraries / apis, etc. .).

0


a source


First, as pointed out, C and C ++ are two different languages. C ++ was created as a superset of C, but both languages ​​have evolved since that time, and some constructs allowed in C were not in C ++ and vice versa.

If you want to learn a language, choose one first and focus on it. You cannot properly study any of them while studying each other at the same time that you will simply confuse them (unless your brain is different from my brain in different ways;)).

You will soon realize that there are 3 different areas in programming:

  • Technic: how to implement what you mean in your language of choice (e.g. C ++ - object oriented or general programming)
  • Design Patterns: High-level view of code / dependency organization (how to split your code so that changing one tiny bit doesn't have an impact across the project)
  • Algorithm: Learn about algorithms and data structures, understand the complexity (time / space)

Of course, the real difficulty is that the 3 areas interact with each other, so you can't really learn about them in isolation and why you need to choose a programming language to be able to experiment with the last two.But don't focus solely on the technical, by heart learning C or C ++ will not make you a good programmer, it will make you a good person who needs an architect to direct your work.

Thanks to the rise of the internet, you can probably find your mistakes there and ask on websites (like this one) when you just can't find out for yourself, whether they are technical errors (compilation errors, program crashes) or design / algorithm errors (although they are difficult to detect as they usually work, it is just either inelegant or slow)

The last word, it's not because it works, it's the only way to do it. You should try and come up with different ways (exploring different paradigms) so that you can increase your experience and get a good idea of ​​which paradigm is being used for which task, when they are appropriate or awkward, etc.

0


a source







All Articles