53 points · by zuck9 · 12y ago · 58 comments
C/C++ is different, I'm afraid of pointers. I can't think of anything that I will like to make in C/C++ since it is so low-level. What I can do in one line in Ruby will take dozens.
I will really appreciate any pointers on how do I learn them. (no pun intended)
Before you start, you need to answer yourself why you're doing it. Unless you really have some inherent interest in the language, learning it just for the sake of learning will be tough because there is no instant gratification.
A good book to start learning C++ is Stanley Lippman's "C++ primer". Make sure you have the latest edition because it reflects the recent big changes in the language.
The best thing you can do when dealing with lower-level languages like C and C++ is understand that things like lists, strings, integers, floating point numbers, objects, etc. do not actually exist. They are simply abstractions. There are only the CPU and memory. You can think of the memory as a very very long array of bytes.Pointers are simply indices for that byte array. Roughly speaking, all that happens in a computer is the CPU fetches some bytes from memory which contain instructions and then modifies the bytes in some other region of the memory according to given instructions.
Once I realized the above things, a lot of other things immediately started making sense to me, so I hope this helps you or others.
Fundamentally, though, they're a value just like any other, just a number that's an index into the giant array called memory. The language provides convenient syntactic sugar to 'dereference' them, which just means evaluate the address and index to it.
Basically:
*blah
is equivalent to: all_of_memory[blah]
which, incidentally, is equivalent to: 0[blah]
and back around again: blah[0]Having said that, C++ has come a long way, despite what haters say. I program primarily in C++ and very rarely have to use pointers. C++11/14 has excellent features (lambdas, initialization lists etc) which can make programming almost seem like writing a Python or Ruby script.
You may also have some luck with Bjarne's smallish "Tour of C++" book -- http://www.stroustrup.com/Tour.html
http://msdn.microsoft.com/en-us/library/hh279654.aspx
If you primarily use value and reference semantics, RAII, smart pointers, STL containers and algorithms, etc, C++ looks a lot like a really powerful high-level language, because that's what it is. Things only start to get ugly and/or low-level when you are trying to heavily optimize things.
Stroustrop (mentioned in other comments) is a great way to get started, and Effective C++ (http://www.amazon.com/Effective-Specific-Improve-Programs-De...) and its cousins will help you not shoot yourself in the foot.
C is a whole different beast. If you've coded in Java, you have a basic idea of how to do encapsulation in C++. Encapsulation in C is nearly impossible to guarantee. Really, a lot of C programming relies heavily on negotiated conventions and design by contract. Learning the C language is pretty easy (read K&R some weekend), but learning to program well in C is a completely different thing.
You really want to work with people who have done C++ before, because there are a number of tricks (RAII, ownership conventions, using const references and references to signal ownership conventions, smart pointers, unique_ptr, etc.) that are common industry practice but aren't all that well-documented on the web. C++ differs from Java or Python in that often the hard part isn't figuring out how to do something, but figuring out what not to do.
This is only half of it. The second half is to choose a mid size project, write yourself something that resembles a spec, and code it up from scratch. It could be some sys util or perhaps a video game (this will require you to learn library linking).
The third half is to read online articles and tutorials while you do the two things above.
The fourth and final half is to get yourself a book once you have a grasp on the language(s) and learn some of their more complicated idioms and pitfalls.
Also as a side note, at this point in time I don't think the "C/C++" conglomerate make much sense any more as modern C++ looks very different from C. Unless you explicitly need C, I would suggest learning C++ first as it's higher level (and also much larger). Though, idk, maybe learning C first is better as it's the foundation for C++... I guess either is fine, just not both simultaneously.
https://stackoverflow.com/questions/3016107/what-is-tagged-s...
http://www.stroustrup.com/C++11FAQ.html
Also, the Boost library is worth a look. C++ is fairly expressive if you wield it properly, and it offers several ways to minimize the pain of memory management. I would hesitate to call C++ "low-level". (C is another story.)
Other than that - C/C++ are used to solve different problems from Python, Ruby, etc. It is a common problem that people used to a particular toolset have difficulty identifying tasks that are suited to other tools. The suggestions to write C extensions or ports of command line tools are good ones - these will give you a taste for where C/C++ shine on a much more gradual learning curve than trying to write device drivers or kernel extensions.
Another example of practical C++ usage: in the past, I've often prototyped algorithms in Python, then rewritten them in C++ once the major bugs are ironed out. YMMV, but I found this to be faster than writing directly in C++.
Pointers may be scary but they are just a concept that you need to grasp, like recursion. You first read about it and kind of get it, then you apply it over and over until suddenly you realize that you have developed a true understanding that allows you to build new abstractions on top.
You will hear people telling you to start with C first because C++ is a superset of C, but I always advice against this. Memory management is "manual" in both, but the idioms you apply are very different between the two. Unless you are looking to truly spend years perfecting both, pick one and go with it.
C is much better as glue code between languages than anything else.
Actually, I think the best approach would be to pick a project for which you can construct a believable (by you) reason why it should be in C. Some possible reasons:
1) You need precise control of hardware
2) You're running in an environment where higher level languages are not available
3) You need precise control of latencies
4) You need the absolute maximum possible performance
There are various projects with these requirements of various difficulties. I would say that it might be hard to simultaneously convince yourself that a project needs the maximum possible performance and also that it is tractable for you as a newbie C developer.
A simple kernel module might not be a bad thing, if you can do your development in a VM to recover from the inevitable crashes. The reason for C will be quite clear, but tooling will suffer.
Targeting microcontrollers will be similarly well motivated at a higher degree of awkwardness.
For instance, in assembly you push/pop from stack, or you move things into the registers and from memory, you will have to deal with memory address, etc..
So he will learn the most important background about thinking in C, like stack vs. memory allocation, and what pointers really are under the hood (the memory address of a thing)
You can always go one level down, but I would hardly say you need assembly for c or c++.
Some useful reading:
http://dan.iel.fm/posts/python-c-extensions/
https://blog.jcoglan.com/2012/07/29/your-first-ruby-native-e...
you can also try writing the core utils using your newly learned C/C++ skills.(https://github.com/chaitanyav/cprograms)
Also read some reviews of the book before you spend any time reading it. The mid 90's were a really bad time for C++ in my opinion since it was kind of a new language and being pushed heavily as the default 'learn computer programming' language (which Java later usurped). This means there are tons of very badly written and confusing resources that do more harm trying to teach C++ than actual good. Stuff like Deitel's C++ How To Program, etc.
For C++ in particular I can recommend:
Thinking in C++ (http://mindview.net/Books/TICPP/ThinkingInCPP2e.html) Although it's getting a little old, I think this is a great resource for someone just picking up the language.
A Tour of C++ (http://www.stroustrup.com/Tour.html) Great small intro to modern C++ programming, by the creator of the language.
http://www.amazon.com/Memory-Programming-Concept-Frantisek-F...
A rather expensive book but stellar reviews. I borrowed it from the library. It's very concise too.
For C++ a lot of people still recommend "Accelerated C++":
http://www.amazon.com/Accelerated-C-Practical-Programming-Ex...
because it teaches you "canonical" C++ instead teaching you "C with classes," which seems to be a common complaint among veteran C++ programmers. It's very readable too.
I'm going to pick up "Writing Great Code":
http://www.amazon.com/Write-Great-Code-Understanding-Machine...
because it explains computer architecture. Once you start programming in C/C++ you are much closer to the metal and having an understanding of the architecture will lead to better choices.
It's doing a great job of teaching me C, while also reminding me how great high level languages actually are!
>>> a = {'foo': 0}
>>> b = a
>>> b['foo'] = 1
>>> a['foo']
1
>>> id(a)
140428021358832
>>> id(b)
140428021358832
`a` and `b` are just pointers to the underlying structure. The only difference is that Python is taking care of the dereferencing for you, whereas in C you would have to explicitly dereference the pointer.That's all there is to them. It's a reference to another location in memory, which, when dereferenced, refers to that other location. In pseudocode:
>>> b = 1
>>> a = &b
>>> b = 2
>>> a*
2
>>> a* = 3
>>> b
3Also, take a look at DCPU-16 stuff. Assembly might be more fun to play around with. http://www.dcpu-ide.com/ Also x86 assembly. Its exciting because you can get incredible performance and get insight into how things work at the lowest levels. Google x86 assembly tutorial. Also the DCPU-16 has awesome peripherals like the display and even a 3d vector display.
Then when you can come at it from the lowest level pointers are less of a big deal. You can think about making your own OS, (look for a tutorial) and then it will be a more natural progression to C (with pointers).
Let me address the motivation issue.
C/C++ strength is speed. Write a simple piece of computation in your favorite language (Ruby for instance). Write the same in C and measure the time it takes.
Once you see how slow Ruby is, you may get excited by C enough to continue exploring it.
Try simple data structures first. Having linked list nodes that actually uses minimal memory (pointer to next node, a data field) is actually pretty exciting. Implement all the operations (get index i, remove index i, etc) because that's where a lot of the insight will come from. Then you can move onto binary search trees and other more complex stuff.
One of the most useful insights I've read was from "Effective C++", which said (paraphrasing) "C++ is a federation / family of languages which work together". Learn the C subset of it first so you're not overwhelmed with available features, and then you can get back to using templates / STL / the other languages later.
IMO you don't need to know either C or asm to be a great programmer. Read a book on operating systems instead and you'll know all you need to about hardware. Add a thorough coverage of algorithms and data structures, and you're in a better position to write effective software than many I've worked with.
It's an excellent book (see the ratings on Amazon) and also fun to read through.
C should be much easier to learn than C++. The language is much more basic feature-wise and the standard C library is very small indeed.
[0]: http://www.amazon.com/Programming-Modern-Approach-2nd-Editio...
I thought of all the conveniences of Spring injection, a bazillion java libraries nicely managed by maven etc. The ecosystem available to me in Java is what always made me give up. I guess I never needed to learn C/C++ badly enough to justify the time investment.
Pick something fun to write (to you) that is normally written in a lower level language.
For me C and C++ were fun to learn by writing graphics applications (ray tracers are great for this) and physics simulations (a solar system simulation maybe). Optimizing these to be as fast as possible is a great way to learn about pointers and manual memory management.
Also, I think pascal is great (I use it professionally/Delphi) and the way how pointer and raw code is used is not scary.
Edit: I'm sorry if this sounds condescending, but I really mean it. Basic is about one step above assembler - you get variables and nice access to some basic IO, but it's much closer to the metal than C. It really doesn't even have functions, you have to do GOSUB/RET yourself. It's also an extremely simple language. But it will give you an idea on how computers work at a basic level. After that you should have a much easier time grokking C.