GCC begins move to C++
GCC begins move to C++
Posted Jun 1, 2010 16:36 UTC (Tue) by dgm (subscriber, #49227)In reply to: GCC begins move to C++ by rev
Parent article: GCC begins move to C++
return codes don't change control flow. That's the reason avik had to introduce auto_ptr<>
and RAII besides exceptions in his example above.
consider for instance:
void f(void) { T * p = malloc (sizeof (T)); g(p); free(p); }
This code does not leak memory in C, but can leak in C++ with exceptions.
Remember we are talking about a _large_ amount of C code that was not written with exceptions in mind.
Posted Jun 1, 2010 17:03 UTC (Tue)
by avik (guest, #704)
[Link] (2 responses)
1. Pick a function
If correctly applied, an entire code base can be converted with very little explicit try/catch blocks remaining.
Posted Jun 1, 2010 17:23 UTC (Tue)
by dgm (subscriber, #49227)
[Link] (1 responses)
Posted Jun 1, 2010 18:01 UTC (Tue)
by avik (guest, #704)
[Link]
Posted Jun 2, 2010 11:13 UTC (Wed)
by gowen (guest, #23914)
[Link]
Posted Jun 2, 2010 18:01 UTC (Wed)
by Cyberax (✭ supporter ✭, #52523)
[Link] (3 responses)
Is it better than a thrown exception?
Posted Jun 3, 2010 21:07 UTC (Thu)
by khim (subscriber, #9252)
[Link]
This is exactly why you CAN'T write such code in gcc. malloc is forbidden there at include file level. Instead you'll use xmalloc - it can not ever return NULL, so there are no need to check the error code.
Posted Jun 4, 2010 9:46 UTC (Fri)
by jrn (subscriber, #64214)
[Link] (1 responses)
On Linux, this is true but mostly irrelevant. Most systems overcommit (it is hard to find a saner thing to do if the user wants large programs to be able to fork() but does not provide the swap to back them) and malloc almost never fails.
Posted Jun 7, 2010 9:47 UTC (Mon)
by nye (subscriber, #51576)
[Link]
Posted Jun 3, 2010 5:44 UTC (Thu)
by njs (subscriber, #40338)
[Link] (1 responses)
You're assuming that g-written-in-C cannot fail, while g-written-in-C++ can, and then saying that that makes C better... which I guess is true given your assumptions, but if you have a method to write error-free code in C then you should share *that*, it'd be a much more compelling argument!
Posted Jun 3, 2010 16:41 UTC (Thu)
by dgm (subscriber, #49227)
[Link]
What I assume is that g-written-in-C cannot change the flow of control. That assumption doesn't hold in C++ with exceptions. That doesn't make C better, just different. Different enough, in fact, that converting C code straight to C++ is not that easy.
By the way:
@gowen: it's just an example.
GCC begins move to C++
2. Modify its callers to expect exceptions from it (adding try/catch if necessary)
3. Convert it to throw exceptions instead of returning error codes
4. Modify it to expect exceptions from its callees (removing try/catch if necessary)
5. Repeat
GCC begins move to C++
GCC begins move to C++
How about...GCC begins move to C++
void f(void)
{
T p;
g(&p);
}
GCC begins move to C++
You are right of course...
malloc() failure
malloc() failure
GCC begins move to C++
GCC begins move to C++
@Cyberax: maybe g() can handle the NULL case gracefully, as it should if properly written.