Ian said:
I'm sorry, but it isn't. RAII is one C++ feature that C can't do.
I respectfully submit that you haven't thought
that though. I won't bore you with the sea stories...
but it's been done...
What Stoustroup provided was an artifact that created a culture
that tried to solve these problems. He did a great job.
Ah! You're avoiding my carefully chosen weasel - "much less than".
It can be used anywhere a resource has to be managed. It
avoids the goto spaghetti often seen in C code to handle an allocation
failure in a block of allocations.
People were successfully avoiding that spaghetti decades ago
with much less powerful tools.
So write a state machine, with one allocation per state and do
*absolutely nothing else* until it terminates. It's the software
equivalent of "get the money up front." If you have
additional allocations later, just add states. Push successful states on
a stack, and unwind on failure. Bada boom, bada bing.
for extra credit, have the allocation requester extend the state
machine in an abstract way. Use the __FILE__ and __LINE__ macros
also in this stack, and write a dump routine. Presto! Asymptotically
full memory leak disclosure.
And then, to be a total creep, use a 'C' macro
to substitute your scheme for malloc()/free()....
I'd allocate the stack thingy very, very statically....
"But I don't want to write a state machine!" Well,
you're not all that concerned about managing complexity
then, are you?
And I want a pony, too.
I mean absolutely no offense, but comments like yours sound like
learned helplessness to me. Break those chains! Take control
of your destiny! Power to the proletariat!
Strike that last one. But really, the project I first
did that on was out of control and this helped stabilize things
*immensely*. Operation without instrumentation is like using
a credit card without reading the billing statements.
You obviously haven't looked at kernel and library code!
Heh. The One True Kernel, and Linus is its prophet?
Oh, I have. it's *a* way. May you never hear the words "We
want you to write a patch against the bridge code..."
It's
good code, but very branchey - you end up following lots of chains.
*Generally*, that sort of activity is unnecessary.