News for hackers. Stuff that matters.
Re:C++ For C Programmers by skrebbel on Monday October 14, 2013 @07:10AM (#6546546)
Nice! However, I'd appreciate the converse more.

I'm pretty well versed in C++, and every time I try to write something in C, I fall back to C++. I find memory management a pain without RAII, error handling a hassle without exceptions, and, well, anything really a pain without some sort of standard container library.

I have the idea that there's good alternatives for these in C-land, but I'm unfamiliar with them and would love for an expert to teach me.

In general, I wonder whether there's really that many people who know C but not C++. This might just be me projecting myself onto the world, though. Anyone?

Re:C++ For C Programmers by luikore on Monday October 14, 2013 @08:01AM (#6546707)
There's no replacement for exceptions, but you can use setjmp and longjmp if you really want to jump out many layers of nested calls.

In C11 there is type generic macros that helps writing C++-like overloaded functions.

There are several extensions in GCC too.

For RAII-like memory management, there are __attribute__((constructor)) and __attribute__((destructor)).

C++ does type reflection on constant expressions via type traits templates, in GCC there is simply typeof().

If you are bored about writing static function modifiers, you can write a function inside function.

If you want to allocate a vector which won't change capacity and be released when out of local scope , for example in C++:

    auto v = std::vector<int>(n);
With C99 dynamic arrays you can use this instead:

    int v[n]; // n is a variable determined at runtime
You can use the union trick to achieve safer type casts too.
Re:C++ For C Programmers by roel_v on Monday October 14, 2013 @08:15AM (#6546749)
Use GObject (https://developer.gnome.org/gobject/stable/) and GLib. GObject is basically a bunch of macros to let you write OO code in C, GLib is a cross-platform set of libraries with somewhat of a 'standard library' of data structures, string handling stuff etc. It's the foundation of Gnome, it's also LGPL so that may be an issue. To be honest though I've never used it for strictly C-only code, I've only used it on Linux with gcc and it was 10 years ago.

Still, it's a nice hack for when you want to write C++-like code while still using C constructs only.

Re:C++ For C Programmers by skrebbel on Monday October 14, 2013 @08:41AM (#6546862)
Thanks for the suggestion, I'll look into it.

That said, my first underbelly reaction is that I don't want to write C++-like code in C. Why would I do that? I might as well use C++ then. It's like writing functional code in Java. It's possible, but it's a hack.

What I'd like to learn is how to decently deal with memory, exceptional behaviour, common data structures, etc in modern idiomatic C. Some other people in this thread made some nice suggestions for that, btw, so thanks guys :)

Re:C++ For C Programmers by frou_dh on Monday October 14, 2013 @03:42PM (#6549184)
May a thousand home-made hash tables bloom!
Re:C++ For C Programmers by yoodenvranx on Monday October 14, 2013 @07:29AM (#6546611)
I kind of like the way how Zed does the error handling:

http://c.learncodethehardway.org/book/ex20.html

Re:C++ For C Programmers by wrl on Monday October 14, 2013 @07:35AM (#6546630)
Here's my preferred way of handling errors: https://www.securecoding.cert.org/confluence/display/seccode...
Re:C++ For C Programmers by paulbarker on Monday October 14, 2013 @07:34AM (#6546625)
I've done so little C++ so long ago that I may as well fall in the category of a C programmer who doesn't know C++. If I had spare time over the next 8 weeks I'd definitely do this as a refresher course.
Re:C++ For C Programmers by ksk on Monday October 14, 2013 @09:14AM (#6547013)
>I find memory management a pain without RAII,

That statement is true. But, as you may be aware, it doesn't encompass quite a few use-cases. A lot of usecases require you to have fine control over performance and using RAII will cause random speed-bumps. for e.g. when a ref-counted smart pointer(s) goes out of scope and triggers 'heavy' destructors on held objects to free up resources - at a non-deterministic time.

Re:C++ For C Programmers by amboar on Monday October 14, 2013 @07:57AM (#6546689)
The collection maybe a little eclectic, but ccan might interest you: http://ccodearchive.net
Re:C++ For C Programmers by udp on Monday October 14, 2013 @09:28AM (#6547088)
> anything really a pain without some sort of standard container library.

It's certainly not standard, but I wrote a nice little generic list library you might be interested in: http://github.com/udp/list

It uses GNU extensions, but can fallback on C++ instead if you need to build with MSVC.

Re:C++ For C Programmers by 16s on Monday October 14, 2013 @09:37AM (#6547135)
I use C++ because of the std containers (vector, map, set, etc). I don't have to roll my own and I'm free to think about the problem at hand rather than how to get the proper data container for this, that and the other.
Re:C++ For C Programmers by finnw on Monday October 14, 2013 @09:38AM (#6547143)
I can understand this - I know some C++ but I am not very good at it, so I usually go for C with "greenspunning", i.e. using an embedded scripting engine, e.g. guile/lua/spidermonkey
Re:C++ For C Programmers by CraigJPerry on Monday October 14, 2013 @07:28AM (#6546609)
You're the first person i've heard of who falls back to C++, if that data point is of any value.

AIUI exceptions and RAII are mutually exclusive in C++. I don't know enough about C++ so i wouldn't be surprised to find out this is wrong.

Re:C++ For C Programmers by jacobparker on Monday October 14, 2013 @07:56AM (#6546684)
> AIUI exceptions and RAII are mutually exclusive in C++

That is... very wrong. Exceptions are perilous unless you use RAII religiously (which you should; it's good.)

For more information, read up on RAII and search for "exception safety."

The definition of C++ RAII from Wikipedia: "In this language, if an exception is thrown, and proper exception handling is in place, the only code that will be executed for the current scope are the destructors of objects declared in that scope". Without this it is very hard to reap any benefits of exceptions (people have disagreements on what those are, if any, but that's another topic).

Re:C++ For C Programmers by CraigJPerry on Monday October 14, 2013 @06:51PM (#6550139)
Up-voted a few of the others below too.

I also found this which I thought was a high quality summary: http://www.parashift.com/c++-faq-lite/ctors-can-throw.html

Re:C++ For C Programmers by archangel_one on Monday October 14, 2013 @07:35AM (#6546626)
Exceptions and RAII match well, actually. Managing resources via RAII means that when an exception is thrown, the destructors of local objects are invoked so they'll release resources they've acquired.

In fact, I'd argue that you very often have to use RAII with exceptions, since there's no 'finally' block on a try-catch statement and hence it's the easiest way to ensure stuff gets cleaned up (well, except for catch(...) and rethrowing, but that's kinda ugly).

Re:C++ For C Programmers by eliasmacpherson on Monday October 14, 2013 @08:19AM (#6546766)
correct me if I am wrong but you can't rethrow in a destructor and hence RAII is required if you are using exceptions - because the cleanup and reporting work must be done in the destructor - unless you use global state to track failures and check after every attempted release of a resource i.e. C error checking.
Re:C++ For C Programmers by pjmlp on Monday October 14, 2013 @08:23AM (#6546786)
> You're the first person i've heard of who falls back to C++, if that data point is of any value.

Every time I write C code is at a gun point of being forced to do so. When I have the choice between C++ and C, I always pick up C++.

Re:C++ For C Programmers by berkut on Monday October 14, 2013 @08:34AM (#6546832)
I've tried several times to write new projects of mine from scratch in C just for a challenge, and have gone back to C++ every time.
Re:C++ For C Programmers by cldr on Monday October 14, 2013 @08:24AM (#6546793)
Not sure what AIUI means, but the very words "exception-safe" usually imply RAII.