tags:

views:

359

answers:

10

i have been programming in C/C++ for my academic courses a lot and was under the impression i had a pretty good grasp of it. but lately i had to work in a bluetooth application that had a server and client implementation in a Linux box and an embedded system. i learned bluez bluetooth API, socket/network programming and coded it.

however i ran into a lot of problems with memory leaks and segmentation faults and other memory related errors along the way.as the code grew more complex i all but lost control of the pointers and threads and sockets. this got me wondering that i had a lot to learn that they didn't say in the basic C/C++ books. so i wanted to ask for the resources that are available that'll help be code better in a professional way in C/C++ .especially for the Linux/Mac environment (gcc compiler).

Edit: changed C to C++ because of the confusion it was creating.

+4  A: 

If you malloc and forget to free, you have a memory leak, what else do you need to know to avoid them? It's all about design patterns, these are quite language agnostic though...

Segfaults are another story of course... If you really want to know how to code C properly, go pick up a copy of the C99 standard. I can't begin to be partially confident in anything I write in C unless I know exactly what the standard says about it. Pretty much the same goes for C++.

EDIT If you are just a beginner at C or C++, you might want to read a book on them first, otherwise the standards will probably be hard to understand.

Longpoke
well of course i know to free after i malloc. but i am sure anyone who tried coding a program of any complexity in C++ has run into memory leaks as other factors come into play like global variables and function pointers and threads and all. i just wanted to learn better practices to minimize this.
sfactor
Longpoke
+1  A: 

I can't comment on C but with C++, getting a good book SO definitive book guide and starting with smaller projects are a great start to learning proper coding techniques.

Robb
A: 

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

RandyMorris
+4  A: 

This question is too big, way too big.

In short, there's little you can do other than keep going. You're going to get hit by segfaults many more times. The only thing you can do is keep the focus, hunt those bugs tirelessly, and always trust the bug is in your code rather than in the compiler or some solid library you use. When you're stuck, post here you specific, narrow questions with the relevant code attached. We'll help you then.

Now, from here on it really depends on what language you use: C or C++? These are so different words won't suffice. If it's C++ you're on then the first advice I'll give is use RAII all the time. If it's C you're using then always be conscious about what owns that pointer, when and where it frees it, and most importantly where does the pointer point to. Also, always initialize your data, especially pointers. Never mind performance before it's time, except for big-oh performance.

That's it. Other than that, post your specific issues and we'll work them out. That's the Right Way™ to learn.

wilhelmtell
A: 

For resource management and avoiding memory leaks, the RAII pattern and smart pointers are essential in C++. Browsing Marshall Cline's C++ FAQ Lite is also invaluable for learning the nuances of the language.

Wyzard
+1  A: 

I see from your comment that you think memory management is the same in C and C++. It's in fact a very different story.

Thanks to exceptions in C++ you get a new and better way of managing the error checking and resource management. The standard practice is called RAII (Resource Acquisition Is Initialization).


For starters read this thread: whats-the-difference-between-c-and-c

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

bitc
+3  A: 
Owen S.
+1  A: 

A lot of it is going to be learn by experience. You can read and read but many times you just need to dive in. The one thing that I will say is use a symbolic debugger. Setting up breakpoints and seeing exactly what all of your variables are will speed up finding problems and fixing them 10-fold.

Flamewires
A: 

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

0.) C++ is not a superset of C, C and C++ are two separate languages with distinct 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. Exploring new material and reviewing what you've already learned is essential to programming in C or C++.

3.) Programming in C or C++ requires not only a knowledge of the language, but also a knowledge of the concepts behind the language, the tools commonly used to develop in the language (debuggers, build toolchains, compilers, libraries/apis, etc).

wash
A: 

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

If you want to learn a language, pick one of them first and focus on it. You can't properly learn either of them while studying the other at the same time as you'll just keep confusing them (unless your brain is very differently organized from mine ;) ).

You'll soon realize there are 3 different areas in programming:

  • Technic: how to realize what you have in mind in your language of choice (C++ features Object-Oriented or Generic programming for example)
  • Design Patterns: high-level view of the code / dependencies organization (how to decouple your code so that changing one tiny bit does not have repercussions throughout the project)
  • Algorithmic: Learn about algorithms and data-structures, understand what complexity (time/space) is

Of course, the real difficulty is that the 3 areas interact with each others so that you can't really learn about them in isolation and that's why you need to pick up a programming language to be able to experiment with the last two. But don't focus solely on technic, learning the C or C++ standard by heart won't make you a good programmer, it'll make you a good technician who needs an architect to direct his work.

Thanks to the advent of the Internet, you can probably search for your errors there and ask on websites (such as this one) when you just can't find out by yourself, be they technical errors (compile errors, program crashes) or design / algorithmic errors (though those are difficult to spot since it usually work, it's just either inelegant or slow)

A last word, it's not because it works that it's the only way to do it. You should try and come up with various ways (exploring various paradigms) so that you can increase your experience and get a good feeling of what paradigm use for what task, when they are suitable or clumsy etc...

Matthieu M.