Live data from Hacker News

Ask HN: How do I learn C and C++ when I love high-level languages?

news.ycombinator.com

51–60 of 62 posts

Re: Ask HN: How do I learn C and C++ when I love high-level languages?

#51
Don't be too afraid of pointers - you're already using them if you're using Python when you work with mutable structures.

    >>> 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
    3

Re: Ask HN: How do I learn C and C++ when I love high-level languages?

#52

Find a slow part of your Python/Ruby/Lua application that could use some speeding up and then write an extension for it in C. Some useful reading: http://www.swig.org/ http://dan.iel.fm/posts/python-c-extensions/ https://blog.jcoglan.com/2012/07/29/your-first-ruby-native-e...

Agree with hackerboos, but don't use Swig, use the CFFI in PyPy (it has a CPython port). I recommend writing a bloom filter or an off heap data store. Another option is to hack on the Lua VM or Redis, both are excellent C code bases.

Re: Ask HN: How do I learn C and C++ when I love high-level languages?

#54
post #42

Earlier quoted context omitted.

I don't get it. What do you mean?

Performance reasons

That's much of it. Another reason might be "no one has written a XYZ interpreter for the target architecture", which may be due to performance or just obscurity. Another problem constraint might be pedagogical - if the problem you are trying to solve is "show how to do X in language Y" then of course language Z can't be used - though that example could reasonably be dismissed as trivially true. I'm sure we can come up with some more if we try.

Re: Ask HN: How do I learn C and C++ when I love high-level languages?

#56

Stop being scared of pointers, they aren't magic and you've been using them all along . In most high level languages you are using a pointer for any variable that's not a number. Which is why when you assign one variable to another and modify one it modifies it from the perspective of the other. Because they point to the same object. Fundamentally, though, they're a value just like any other, just a number that's an…

In all my years, I've never seen anyone describe it like this. Nice one.

Re: Ask HN: How do I learn C and C++ when I love high-level languages?

#57
I have many timed tried to embark on this very journey and always gave up when I realized I had none of the conveniences available to me as I do with the Java platform.

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.

Re: Ask HN: How do I learn C and C++ when I love high-level languages?

#58
First, just to clear things up, C and C++ are two different languages. Yes, C++ is backwards compatible, but you'll do yourself a favor if you start perceiving them as two completely separate laguages now.

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.

Re: Ask HN: How do I learn C and C++ when I love high-level languages?

#60
Take a look at Nim. It is extremely efficient (since it compiles to C) and clean, and will allow you to incredibly easily interface with C or go down to the low level.

http://nim-lang.org/

Also, 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).

Post reply on HN