128 points · by jamesbritt · 13y ago · 151 comments
Aside from the ability to share code, it also makes sure that we are not using anymore memory than we really need. Avoiding unnecessary GCs is an important concern in writing performant Android Apps. Having the core C layer only hold on to as little memory as possible is a huge win.
On a less objective note: When I left undergrad I thought I'd never write C again, but it was such a pleasure working with it again. Knowing (almost) exactly what operations your code is doing is quite liberating after working with so many other higher level languages and frameworks.
This is exactly why I still enjoy programming in C so much. Even compared to other languages that I adore and voluntarily use often, programming in C and programming in anything else feels like the difference between treading water in a swimming pool, and treading water in a murky lake. I just have a just a much greater, rather comforting, sense of situational awareness while using C.
MonoTouch/MonoDroid are not in my area of interest, since I prefer to have full sources available for everything, and I see no reason to buy such a tool when, with Lua+C/C++ I can just as easily build it myself ..
Why? Because while others are writing Javascript-like languages that compile to Javascript, debating the merits of Rust vs. Go, insisting that a language without monads is unusable, arguing over whether a language without support for multiple inheritance can be "real OO," claiming that homoiconicity leads to mystical union, and proudly stating that x language is "almost as fast as C" -- while all of this is going on, there are legions of people who have been quietly building nearly every significant piece of the infrastructure of modern computing in a language that is now over forty years old.
They don't whine, they don't go chasing after "frameworks," and they don't succumb to the twenty-one-days model of learning. They just build shit. Lots and lots of it. In fact, you're all soaking in it.
Like I said, it's a religious subject. But as for me and my house, we will use C.
I had to upvote you just for that. (Not that I necessarily agree ... just that it's a hell of a clever ending).
I love C above almost all languages you subtly referenced. I disagree with a lot of what you said, and a lot of what you implied. I just wish I never become that guy.
But hopefully you will, and I can tell you why.
After a long and painful apprenticeship, you finally realize that being a great programmer has nothing to do with the language or its features. You get to the point where you're completely done learning this or that new technique (which, by the way, is nearly always an old thing for someone else). Your "profound enlightenment experience" with Lisp is ten years old; your copy of Design Patterns looks like an old Bible; you've had dozens of dirty weekends with stack-based languages, declarative languages, experimental languages, etc. You've been all of those places and done all of that. Many, many times.
At that point, the pros on cons have fully evened out, and what you want is a tool that you can completely and totally master. You've stopped searching for tools without warts, edge cases, and gotchas, because you know those do not exist. What matters is community, ancestral memory, stability, maturity, docs. Above all, you just want to build something really great.
It is at that point that you become that guy. You might well turn to C, as many before you have. Maybe, maybe not. Lots of us have.
We aren't crusty old neanderthals, though. We're just at the logical end of the process.
For me, now, new school C can be expressed in one word: Lua.
Everything I need C++ to do, I can get Lua+C-bindings to do easier, with less fuss, and more productively in the short as well as long run due to the ease of use of the language. For almost every modern project I've worked on, the combination of a powerful C library collection and the Lua VM wrapper to make it all scriptable has been absolutely unbeatable.
Sure, I write Python .. I've done more than my share of Ruby projects, and I could definitely spend a few years working in pure C++ if it were necessary. But the point is that none of these languages have been necessary recently, with my Lua+C skills ..
Generating the Lua bindings is very easy - there's a tool called ToLua++ which reads a simplified C++ header and generates the C code for the bindings.
A really good example for you is MOAI: http://getmoai.com/
Look underneath the covers (i.e. build MOAI from sources) and you'll see all of the techniques I'm promoting, albeit in an open, easy to understand - and DAMNED POWERFUL - form.
MOAI is a cross-platform game development toolkit, but since its been designed by talented engineers with great ideas, it could as easily be used to build a next-generation Operating System, akin to what is happening with Android and the Dalvik VM.
The Linux Kernel + MOAI(Lua) would be a formidable challenger to the Android eco-system, imho, given a little more love .. I've already gotten my rPi booting directly to the MOAI runtime, anyway, and its a much more fun way of writing apps for the thing than anything else right now, at least in my opinion. With the advantage that those same apps, with very, very little effort, will run on Windows, Linux, OSX .. iOS .. Android .. Chrome Native Client .. and so on ..
MOAI is an example of the ways in which the LuaVM can actually fulfill the promises made back in the Java honeymoon days ..
There are some great tutorials on this, here are a few I found with google-fu:
http://lua-users.org/wiki/BindingCodeToLua
http://www.nicolasgoles.com/2012/08/03/lua-tutorial---c-bind...
http://www.gandogames.com/2011/01/tutorial-using-luabind-to-...
I think you will see, once you study the above, that there is a vast amount of power available to you. The Lua VM, written itself in pure C, is an utter joy to behold, and I encourage every new C programmer to inspect the Lua VM sources (its only about 12 files..) and see for yourself just what it has to offer you. C+Lua can be a very, very powerful tool .. and anyone learning C today would benefit greatly from also doing some Lua bindings, themselves, to see what I mean ..
Do they not know that basically every important piece of software today is still written in those languages?
Has this realization somehow been lost over time with the rise of languages like Java, PHP, JavaScript and Ruby, even though the major implementations of those languages are themselves built using C and/or C++?
For example, I go on IRC with weechat, which is written in C. An IRC client spends most of its time idling, waiting for network data or input from the user, so certainly it could be written in a language like OCaml.
Nobody wants to boostrap another language in their $HOME just to use IRC.
Rather than repeat the code examples in detail, I'll link to some earlier examples I've shown:
http://news.ycombinator.com/item?id=4724781
http://news.ycombinator.com/item?id=4724421
Basically, they come down to references and iterators not knowing about aliasing, thus being unable to determine when the referent is destroyed.
When do you allocate on the stack vs. on the heap? It matters, and is something you don't have to think about in Python or Java or C#.
Are you passing by value or by reference? Are you passing a pointer by value?
What's the actual behavior when you use the unary * operator? The copy constructor? The assignment operator?
When you use MyType foo = 3;, what's actually happening? Did you mean to use the implicit constructor, or was that a programming error?
And then you have all the warts related to the single-pass compilation and inlining of headers because that's the way C++ does things...and let's not get into the misfeatures that were brought over due to C compatibility, like macros.
So no, C++ is not as safe as any language. It is manifestly unsafe. It can be a bit easier to manage its complexity with C++11, but it's still far from safe. And C++11 makes the language even larger and more complex...
The thing is that you have to clarify safety and danger. In more application than people would like to acknowledge, slowing down to the point of unusability can be as much of a dangerous result as crashing with a null pointer. Slowness has been standard quality of Python and Ruby development as much as null pointers have been a standard quality of c/c++ development. Either can be avoided with proper care but these all somewhat different failure modes.
The one thing is that c++ do present a different kind of security risk than Ruby/Python slow-downs or even Ruby/Python crashes. Despite this, a some point you still have to consider the tradeoffs.
"you need to find a good tutorial that doesn't go low level before you need it."
In other words, don't program in C++ at all. The default numeric type in C++ is fixed-width (int or floating point), the default string type is a primitive pointer (const char *), and you still see low-level pointer types tossed around as iterators (e.g. for the standard vector class).
A blatant overstatement. There's lots of important work done for the CLR (MS, many others), Java (Sun, Google, Amazon, bits of Facebook, IBM, etc, etc) or in Python (Dropbox) or in Cobol (banks...sigh). Yeah, there's plenty of important C/Cpp code but it's probably in a minority of "important", even if important just means "commercial" or "popular".
Surprised? No, I'm terrified. It is scary to think that critical systems are written in poorly defined languages that encourage programmers to create buggy, unreliable software.
"Do they not know that basically every important piece of software today is still written in those languages?"
That is a problem that needs to be (slowly) corrected. We can start by not writing tomorrow's important software in those languages.
A poor workman blames his tools.
Not necessarily. The SPARK compiler was originally written in Ada. Python is moving towards a bootstrapped model with PyPy. Lisp compilers have been written in Lisp. There was a Perl 6 interpreter written in Haskell. The HaXe compiler is written in OCaml:
https://en.wikipedia.org/wiki/HaXe#Compiler_implementation_a...
"your argument seems to suggest that it's impossible to write critical systems in these languages"
Obviously it is not impossible, given how many such systems are out there. It is just scary.
"what would the alternative be?"
Well, these guys may be on to something:
https://en.wikipedia.org/wiki/Altran_Praxis
"Surely if this is the case we can't use Linux as it's written in C"
Would you trust your life to the Linux kernel? Would you be comfortable with that? I still see the occasional kernel panic; how would you like a kernel panic that sent your car swerving into a tree?
"we can't use Java because it's C++ based..."
...or we bootstrap our compilers and programming systems, and let C++ fade away (as it should). What stops us now is the volume of code, but we could go a long way by just not writing more C++ code, so that the problem does not continue to expand.
This feels like an oversimplification. There are surely very good reasons to choose [insert language other than C] for production / prototyping / development purposes. The fact that [language] was built in and / or relies on C libraries is at that point not central to the discussion.
Then I realized... "of course, they only learn Java now". I left for scripting a long time ago, but still: Sad.
http://shop.oreilly.com/product/0636920025108.do?intcmp=il-c...
what I noticed was
> Use Autotools, C’s de facto cross-platform package manager.
Seeing the word "autotools" sends familiar, but scary shiver down my spine. After some years with maven and CPAN, I don't ever want to go back to autotools, no thanks.
Looking at the book's TOC it looks like there's a bunch of interesting ideas in there, I'm tempted to pick it up.
The first half of the book covers Git, autotools, Makefiles, and random topics tangentially related to C. If you're unfamiliar with those, it's an okay introduction to them, but there's some dubious advice in those sections, too.
I know you can find implementations of all of these in C but this is the kind of thing that really needs to be built in to any modern language. C is fine for writing device drivers etc where you can't really assume anything about your environment but for anything else it seems too bike-sheddish.
It seems this term has evolved beyond any recognition to me. In what way is choosing C over another language that may provide more built in "arguing over something trivial/tangential"?
Code reuse at the level of strings and containers is essential for any kind of higher-level programming. C fails here.
On the other hand, as the author said, C is over 40 years old. Many consider that a weakness, but he considers it a strength, given the large number of C libraries available, whereas you don't have the same number of libraries with native Go bindings. Ultimately, of course, it comes down to using the right tool for the job. That may be C, that may be Go, that may be a higher-level language like Python, Ruby, etc.
Don't confuse "easy" with "simple". That's my latest mantra.
I think that if it doesn't make you (a) depressed, then (b) mad, that you didn't listen carefully enough. :-)
Do elaborate. I come from the same background and I've been contemplating forking C for several years now. What you said sounds all too familiar to my own reasoning, so I'd be very curious to know what you have in mind. In broad strokes.
The Google guidelines are okay for non-kernel work. I've worked on three teams that have independently converged on very similar guidelines (at Apple, a start-up, and a group in Microsoft). I find that convergence quite interesting.
http://en.wikipedia.org/wiki/I/O_Kit
The kernel itself is in C though.
I think if there were more educational websites teaching C, more people would use it. I think new programmers will appreciate coding a lot more if they learned C instead of using Ruby.
In old school or new school C, there's every reason to minimize the use of malloc and it's easy to avoid.
With a modern VM system, it's not quite as big a deal since your heap is effectively infinite for many applications. It is still an issue on embedded systems, and was a problem back in the day too.
I've heard of both of those approaches and they sound pretty hard, so I'd be interested to hear your thoughts/be pointed to good references on the topic. Thanks in advance.
2. If you use the space of this stack for dynamic memory allocation as well, you will be limiting your capacity to write recursive functions.
3. it will depend on the kind of system you are developing. If you are developing some tool to analyze millions of tweets, you have to use "heap" there is no alternate. If you are developing an embedded application " say loading your list of conatcts for sending the message yes there you can get away with allocating the memory on stack itself. But then there most of the phones do not allow you to make a list of more than 25-30 people.
So it depends on the application one is willing to develop and K&R well recognized it in early 70s itself that is why they introduced alloca in addition to malloc , calloc
This is a waste of time.
We all need to start evaluating the problem, taking account for the environment, and use the language that gets the job done.
That's great, unless your employer/client/etc. is allergic to open source. I really can't consider something part of the language unless it's in a standard library that comes out of the box with the major compilers.
Would this book be a good place to start?
What other recommendations could experienced C programmers offer. I'm not simply asking about the language, but best practices for memory management and toolsand above all, practical advice.
To your request, this is not a good place to start. By the table of contents, it appears to assume a working knowledge of C.
Here are some ideas you can use to design your own "teach me C" course.
Read K&R, and write some simple toys. Write your own "cp". Handle some errors, add some options. Try out fread(3)/fwrite(3), then try read(2)/write(2).
Then write "mv" and do the same. Now make it fast within a filesystem. Or work across filesystems (depending on your first implementation).
Then write an encryption or compression program.
Now write a simple library, make a red/black tree. Store ints first, but then later, store generic objects (design your API carefully!). Make two iterators, one using a callback and one using a next() function. Decide which you like better.
About now you may start to find your development environment lacking, do you use Make? Autotools? Something else? Go learn one and update your old projects to use it. While you're at it, write some tests for them. Did you handle command line options for your version of "cp"? Did you use getopt_long(3)? Might be a good idea. Oh by the way, does it support "-R"? Hmm...and your tree isn't reentrant either, better go learn about pthread locks.
After a while, you should start learning some other libraries (because you want to do real work): curl, libxml, sqlite (hey these are in that book!). Study their code, their build systems, their test harnesses (oh man, especially sqlite's) and incorporate the ideas you like into your own work. Use them to build something cool, maybe a web crawler or whatever.
Do you like music? Learn an audio library and make something. From here, you should direct yourself to what you like building, because you probably have enough tools to do the learning on your own.
HTH
Read K&R, and write some simple toys.
The comp.lang.c FAQ was very helpful when I taught myself C using K&R. It answered most of the questions that I had that I couldn't answer with K&R alone. I ended up reading the FAQ straight through because it was so helpful.> IMHO you're not an experienced programmer.
What prompted you to write this? I'm not insulted. I am curious why you would leap to that conclusion. Is it based on how I worded my question? Or the fact I haven't "learned" or used c? Do you think I don't understand or haven't used pointers? Or memory management issues?
This is true of most languages, and probably most true of Lisps and MLs. If you've never learned a Lisp, I also would consider you "not an experienced programmer". Not "an inexperienced programmer", and you may be "an experienced <ruby/python/garfunkel> programmer", but I think you are "not an experienced programmer". You should also use Perl at some point, for example. By the way, it's awesome that you want to learn more, that's a very good sign. (Again, this is all very heavy IMHO ;-)
In C's case, some of the things you learn are pointers, memory management, portability, simplicity, filesystems, build systems, etc. These are usually things that are handled by other languages, or that other languages discourage you from thinking about. Other languages will have made choices for you about how to hide these details, and it's often useful to have made your own choices about such problems at least a few times in the past. Otherwise you may end up blinded by the choices made by others.
http://www.drdobbs.com/architecture-and-design/developer-rea...
Note that the review is on Page 2 of the article.
http://unix.stackexchange.com/questions/23634/while-there-is...
In no case does C++ require any more runtime than C. You can use little or no runtime at all in either language, with minimal effort.
Easier to write low-level in C? This is unsupportable. C++ can write anything you can write in C (its a superset), so anything possible in C is possible in C++, but now you have more leverage.
So yes, 1000 monkeys on 1000 typewriters can make something large in C. But one guy (or a small team), properly trained and experienced, can move mountains with a high-leverage language.
HN says that Learn C The Hard Way by Zed Shaw is good.
http://www.hnsearch.com/search#request/all&q=%22learn+c%...
http://news.ycombinator.com/item?id=1027549
As with any new language, once you're comfortable with a small core of the language, start working on your own problems. Start with little made up problems, and move on to useful tools as soon as possible. You don't have to be a master to write your own tools, and you won't become a master until after you've written your own tools.