[Info-vax] BASIC (and Horizon)
Arne Vajhøj
arne at vajhoej.dk
Wed Jan 31 17:08:44 EST 2024
On 1/31/2024 4:28 PM, Lawrence D'Oliveiro wrote:
> I have never written a goto in C code (not production code, anyway). These
> days, you need to do so much dynamic allocation, there is nearly always
> some need for cleanup when exiting an inner block anyway, so you can’t
> just jump directly somewhere else first. The overall pattern looks like
> this:
>
> MyPtr obj = NULL;
> do /*once*/
> {
> ... possible other stuff ...
> «allocate memory for obj»;
> if («error occurred»)
> break;
> ... possible other stuff using obj ...
> }
> while (false);
> free(obj);
>
> You can confirm, just by inspection, that there is no path out of the
> block that does not pass through the free() call precisely once.
And the difference compared to:
MyPtr obj = NULL;
... possible other stuff ...
«allocate memory for obj»;
if («error occurred»)
goto lbl_freeobj;
... possible other stuff using obj ...
lbl_freeobj:
free(obj);
are?
Arne
More information about the Info-vax
mailing list