Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #10403

Re: Handing initialization failures

From Nobody <nobody@nowhere.com>
Subject Re: Handing initialization failures
Date 2011-09-17 19:16 +0100
Message-Id <pan.2011.09.17.18.16.29.708000@nowhere.com>
Newsgroups comp.lang.c
References <013d83d0-af71-443a-b9a8-f40b0f28f061@h9g2000yqi.googlegroups.com>
Organization Zen Internet

Show all headers | View raw


On Fri, 16 Sep 2011 11:42:36 -0700, Максим Фомин wrote:

> What is best way to deal whis this?

C++ and RAII ;)

> There is obvious solution to free all
> previous allocated resources within "if" test block of each element,
> however I am looking for a better solution.

One solution not mentioned so far is:

	obj = obj_alloc();
	if (!obj)
	   goto fail0;
	obj->res1 = res1_alloc();
	if (!obj->res1)
	   goto fail1;
	obj->res2 = res2_alloc();
	if (!obj->res2)
	   goto fail2;
	...
	return obj;	/* success */
	...
	fail3:
	    res2_free(obj->res2);
	fail2:
	    res1_free(obj->res1);
	fail1:
	    obj_free(obj);
	fail0:
	    return NULL;

Personally, I'd just go with having all of the _free() functions ignore
NULL pointers, so that you can legitimately call the top-level obj_free()
function on a partially-constructed object.

Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Handing initialization failures Максим Фомин <maxim@maxim-fomin.ru> - 2011-09-16 11:42 -0700
  Re: Handing initialization failures James Kuyper <jameskuyper@verizon.net> - 2011-09-16 15:20 -0400
    Re: Handing initialization failures Максим Фомин <maxim@maxim-fomin.ru> - 2011-09-16 21:50 -0700
  Re: Handing initialization failures Ben Bacarisse <ben.usenet@bsb.me.uk> - 2011-09-16 20:26 +0100
  Re: Handing initialization failures ImpalerCore <jadill33@gmail.com> - 2011-09-16 12:29 -0700
  Re: Handing initialization failures jacob navia <jacob@spamsink.net> - 2011-09-16 21:45 +0200
    Re: Handing initialization failures Ian Collins <ian-news@hotmail.com> - 2011-09-17 08:15 +1200
  Re: Handing initialization failures "christian.bau" <christian.bau@cbau.wanadoo.co.uk> - 2011-09-16 13:41 -0700
  Re: Handing initialization failures Malcolm McLean <malcolm.mclean5@btinternet.com> - 2011-09-16 13:47 -0700
  Re: Handing initialization failures Paul N <gw7rib@aol.com> - 2011-09-17 04:04 -0700
    Re: Handing initialization failures James Kuyper <jameskuyper@verizon.net> - 2011-09-17 15:16 -0400
  Re: Handing initialization failures Nobody <nobody@nowhere.com> - 2011-09-17 19:16 +0100
  Re: Handing initialization failures Lauri Alanko <la@iki.fi> - 2011-09-21 19:09 +0000
    Re: Handing initialization failures Gene <gene.ressler@gmail.com> - 2011-09-22 06:32 -0700

csiph-web