B
BartC
Malcolm McLean said:בת×ריך ×™×•× ×©×‘×ª, 9 ×‘×™×•× ×™ 2012 01:14:43 UTC+1, מ×ת Bart: .....
The central mistake is that the the consturctor takes an empty structure
to fill in, rather than returning a pointer to an allocated structure.
Then the destructor itself can return an error.
Change this, and you get
Init(a); if(x) goto f0;
Init(b); if(x) goto f0;
Init(c); if(x) goto f0;
Init(d); if(x) goto f0;
Set(b,3); if(x) goto f0;
Free(d);
Free(c);
Free(b);
Free(a);
return(p);
f0: Free(d);
Free(c);
Free(b);
Free(a);
return(q);
That's much mpre acceptable. The control flow is still doiminated by
gotos, but that's because all the calls can return errors, which go into a
single error handler.
Yes. And now the two blocks of Frees are almost identical, and they can be
combined into one. At that point you'll probably change the initialisation
to be something like:
if (Init(a) && Init(b) ... )
Set(b,3);
Free(d);
...
However there is the requirement to return one of several error codes,
either from the first failing Init(), from a failing Set(), or from a first
failing Free() (but in case of a failure here, all remaining objects must
still be freed).
It's this elevating the importance of an error return far above that of any
actual results, that is causing the difficulties. The design needs more work
to make it easier to manage allocation and returns, otherwise programming
anything non-trivial (or even trivial!) will be impossible.