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


Groups > comp.lang.c > #21522 > unrolled thread

Error returns and repeated code, GOTOs, etc.

Started bymike3 <mike4ty4@yahoo.com>
First post2012-05-29 22:44 -0700
Last post2012-06-06 08:12 -0700
Articles 20 on this page of 133 — 38 participants

Back to article view | Back to comp.lang.c


Contents

  Error returns and repeated code, GOTOs, etc. mike3 <mike4ty4@yahoo.com> - 2012-05-29 22:44 -0700
    Re: Error returns and repeated code, GOTOs, etc. Ike Naar <ike@sverige.freeshell.org> - 2012-05-30 06:38 +0000
    Re: Error returns and repeated code, GOTOs, etc. Nils M Holm <news2009@t3x.org> - 2012-05-30 06:57 +0000
    Re: Error returns and repeated code, GOTOs, etc. Malcolm McLean <malcolm.mclean5@btinternet.com> - 2012-05-30 00:52 -0700
      Re: Error returns and repeated code, GOTOs, etc. mike3 <mike4ty4@yahoo.com> - 2012-05-30 01:50 -0700
    Re: Error returns and repeated code, GOTOs, etc. MarkBluemel <mark_bluemel@pobox.com> - 2012-05-30 09:55 +0100
      Re: Error returns and repeated code, GOTOs, etc. Willem <willem@toad.stack.nl> - 2012-05-30 09:18 +0000
        Re: Error returns and repeated code, GOTOs, etc. tom st denis <tom@iahu.ca> - 2012-05-30 05:16 -0700
          Re: Error returns and repeated code, GOTOs, etc. Willem <willem@toad.stack.nl> - 2012-05-30 16:26 +0000
            Re: Error returns and repeated code, GOTOs, etc. tom st denis <tom@iahu.ca> - 2012-06-01 08:05 -0700
              Re: Error returns and repeated code, GOTOs, etc. Willem <willem@toad.stack.nl> - 2012-06-03 11:24 +0000
            Re: Error returns and repeated code, GOTOs, etc. jgk@panix.com (Joe keane) - 2012-06-02 13:54 +0000
              Re: Error returns and repeated code, GOTOs, etc. Jean-Christophe <5.d@free.fr> - 2012-06-02 07:24 -0700
                Re: Error returns and repeated code, GOTOs, etc. jgk@panix.com (Joe keane) - 2012-06-02 16:37 +0000
                  Re: Error returns and repeated code, GOTOs, etc. Jean-Christophe <5.d@free.fr> - 2012-06-02 12:46 -0700
              Re: Error returns and repeated code, GOTOs, etc. Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-06-02 15:42 +0100
                Re: Error returns and repeated code, GOTOs, etc. jgk@panix.com (Joe keane) - 2012-06-02 19:35 +0000
                  Re: Error returns and repeated code, GOTOs, etc. Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-06-03 03:53 +0100
                    Re: Error returns and repeated code, GOTOs, etc. Phil Carmody <thefatphil_demunged@yahoo.co.uk> - 2012-06-11 18:10 +0300
                      Re: Error returns and repeated code, GOTOs, etc. Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-06-11 16:19 +0100
              Re: Error returns and repeated code, GOTOs, etc. Martin Shobe <martin.shobe@yahoo.com> - 2012-06-02 19:27 +0000
                Re: Error returns and repeated code, GOTOs, etc. jgk@panix.com (Joe keane) - 2012-06-03 23:48 +0000
                  Re: Error returns and repeated code, GOTOs, etc. Ian Collins <ian-news@hotmail.com> - 2012-06-04 11:53 +1200
                  Re: Error returns and repeated code, GOTOs, etc. Martin Shobe <martin.shobe@yahoo.com> - 2012-06-04 03:32 +0000
      Re: Error returns and repeated code, GOTOs, etc. nick_keighley_nospam@hotmail.com - 2012-05-30 07:25 -0700
      Re: Error returns and repeated code, GOTOs, etc. falk@rahul.net (Edward A. Falk) - 2012-06-05 07:20 +0000
    Re: Error returns and repeated code, GOTOs, etc. Ian Collins <ian-news@hotmail.com> - 2012-05-30 21:50 +1200
      Re: Error returns and repeated code, GOTOs, etc. Malcolm McLean <malcolm.mclean5@btinternet.com> - 2012-05-30 03:34 -0700
        Re: Error returns and repeated code, GOTOs, etc. Ian Collins <ian-news@hotmail.com> - 2012-05-30 22:57 +1200
        Re: Error returns and repeated code, GOTOs, etc. Willem <willem@toad.stack.nl> - 2012-05-30 11:04 +0000
        Re: Error returns and repeated code, GOTOs, etc. nick_keighley_nospam@hotmail.com - 2012-05-30 04:53 -0700
    Re: Error returns and repeated code, GOTOs, etc. "BartC" <bc@freeuk.com> - 2012-05-30 13:28 +0100
      Re: Error returns and repeated code, GOTOs, etc. mike3 <mike4ty4@yahoo.com> - 2012-05-30 13:57 -0700
    Re: Error returns and repeated code, GOTOs, etc. "io_x" <a@b.c.invalid> - 2012-05-30 15:19 +0200
      Re: Error returns and repeated code, GOTOs, etc. Mark Bluemel <mark.bluemel@googlemail.com> - 2012-05-31 01:10 -0700
      Re: Error returns and repeated code, GOTOs, etc. "io_x" <a@b.c.invalid> - 2012-06-02 17:34 +0200
        Re: Error returns and repeated code, GOTOs, etc. "io_x" <a@b.c.invalid> - 2012-06-03 10:33 +0200
    Re: Error returns and repeated code, GOTOs, etc. Ike Naar <ike@iceland.freeshell.org> - 2012-05-30 13:56 +0000
      Re: Error returns and repeated code, GOTOs, etc. Kulin <remailer@reece.net.au> - 2012-05-30 17:29 +0000
    Re: Error returns and repeated code, GOTOs, etc. ImpalerCore <jadill33@gmail.com> - 2012-05-30 07:13 -0700
      Re: Error returns and repeated code, GOTOs, etc. Thad Smith <ThadSmith@acm.org> - 2012-06-02 07:20 -0600
    Re: Error returns and repeated code, GOTOs, etc. Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-05-30 16:37 +0100
    Re: Error returns and repeated code, GOTOs, etc. Johann Klammer <klammerj@NOSPAM.a1.net> - 2012-05-30 19:05 +0200
    Re: Error returns and repeated code, GOTOs, etc. Tim Rentsch <txr@alumni.caltech.edu> - 2012-05-31 13:19 -0700
      Re: Error returns and repeated code, GOTOs, etc. mike3 <mike4ty4@yahoo.com> - 2012-05-31 13:49 -0700
        Re: Error returns and repeated code, GOTOs, etc. "BartC" <bc@freeuk.com> - 2012-05-31 22:08 +0100
          Re: Error returns and repeated code, GOTOs, etc. mike3 <mike4ty4@yahoo.com> - 2012-05-31 16:08 -0700
        Re: Error returns and repeated code, GOTOs, etc. Tim Rentsch <txr@alumni.caltech.edu> - 2012-05-31 17:34 -0700
        Re: Error returns and repeated code, GOTOs, etc. Willem <willem@toad.stack.nl> - 2012-06-01 07:40 +0000
    GOTOs discovered in "real world" program! (was Re: Error returns and repeated code, GOTOs, etc.) mike3 <mike4ty4@yahoo.com> - 2012-05-31 16:39 -0700
      Re: GOTOs discovered in "real world" program! Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-06-01 03:21 +0100
        Re: GOTOs discovered in "real world" program! mike3 <mike4ty4@yahoo.com> - 2012-06-01 03:49 -0700
          Re: GOTOs discovered in "real world" program! Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-06-02 02:15 +0100
            Re: GOTOs discovered in "real world" program! mike3 <mike4ty4@yahoo.com> - 2012-06-01 19:45 -0700
              Re: GOTOs discovered in "real world" program! Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-06-02 15:00 +0100
                Re: GOTOs discovered in "real world" program! mike3 <mike4ty4@yahoo.com> - 2012-06-02 16:44 -0700
                  Re: GOTOs discovered in "real world" program! Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-06-03 03:51 +0100
        Re: GOTOs discovered in "real world" program! "Aaron W.  Hsu" <arcfide@sacrideo.us> - 2012-06-01 12:06 -0400
          Re: GOTOs discovered in "real world" program! Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-06-02 02:01 +0100
            Re: GOTOs discovered in "real world" program! mike3 <mike4ty4@yahoo.com> - 2012-06-01 19:46 -0700
              Re: GOTOs discovered in "real world" program! mike3 <mike4ty4@yahoo.com> - 2012-06-01 21:19 -0700
                Re: GOTOs discovered in "real world" program! blmblm@myrealbox.com <blmblm.myrealbox@gmail.com> - 2012-06-02 16:35 +0000
              Re: GOTOs discovered in "real world" program! Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-06-02 14:50 +0100
              Re: GOTOs discovered in "real world" program! Malcolm McLean <malcolm.mclean5@btinternet.com> - 2012-06-02 07:04 -0700
            Re: GOTOs discovered in "real world" program! "Aaron W. Hsu" <arcfide@sacrideo.us> - 2012-06-02 04:37 -0500
              Re: GOTOs discovered in "real world" program! mike3 <mike4ty4@yahoo.com> - 2012-06-02 03:40 -0700
                Re: GOTOs discovered in "real world" program! Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-06-02 14:46 +0100
                  Re: GOTOs discovered in "real world" program! "BartC" <bc@freeuk.com> - 2012-06-02 17:10 +0100
                  Re: GOTOs discovered in "real world" program! Robert Wessel <robertwessel2@yahoo.com> - 2012-06-02 11:35 -0500
      Re: GOTOs discovered in "real world" program! (was Re: Error returns and repeated code, GOTOs, etc.) "BartC" <bc@freeuk.com> - 2012-06-02 09:46 +0100
        Re: GOTOs discovered in "real world" program! (was Re: Error returns and repeated code, GOTOs, etc.) "io_x" <a@b.c.invalid> - 2012-06-03 10:43 +0200
          Re: GOTOs discovered in "real world" program! (was Re: Error returns and repeated code, GOTOs, etc.) "BartC" <bc@freeuk.com> - 2012-06-03 10:58 +0100
            Re: GOTOs discovered in "real world" program! (was Re: Error returns and repeated code, GOTOs, etc.) "io_x" <a@b.c.invalid> - 2012-06-03 17:18 +0200
            Re: GOTOs discovered in "real world" program! (was Re: Error returns and repeated code, GOTOs, etc.) jgk@panix.com (Joe keane) - 2012-06-04 20:40 +0000
              Re: GOTOs discovered in "real world" program! (was Re: Error returns and repeated code, GOTOs, etc.) jgk@panix.com (Joe keane) - 2012-06-05 01:03 +0000
                Re: GOTOs discovered in "real world" program! Joe Pfeiffer <pfeiffer@cs.nmsu.edu> - 2012-06-04 21:36 -0600
                  Re: GOTOs discovered in "real world" program! jgk@panix.com (Joe keane) - 2012-06-05 15:13 +0000
                Re: GOTOs discovered in "real world" program! (was Re: Error returns and repeated code, GOTOs, etc.) Ike Naar <ike@sverige.freeshell.org> - 2012-06-05 08:12 +0000
                  Re: GOTOs discovered in "real world" program! (was Re: Error returns and repeated code, GOTOs, etc.) James Kuyper <jameskuyper@verizon.net> - 2012-06-05 06:00 -0400
              Re: GOTOs discovered in "real world" program! (was Re: Error returns and repeated code, GOTOs, etc.) jgk@panix.com (Joe keane) - 2012-06-05 02:05 +0000
                Re: GOTOs discovered in "real world" program! (was Re: Error returns and repeated code, GOTOs, etc.) Martin Shobe <martin.shobe@yahoo.com> - 2012-06-05 03:16 +0000
          Re: GOTOs discovered in "real world" program! (was Re: Error returns and repeated code, GOTOs, etc.) "io_x" <a@b.c.invalid> - 2012-06-03 17:30 +0200
            Re: GOTOs discovered in "real world" program! (was Re: Error returns and repeated code, GOTOs, etc.) Jean-Christophe <5.d@free.fr> - 2012-06-03 09:02 -0700
              Re: GOTOs discovered in "real world" program! (was Re: Error returns and repeated code, GOTOs, etc.) Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-06-03 22:37 +0100
                Re: GOTOs discovered in "real world" program! (was Re: Error returns and repeated code, GOTOs, etc.) Ike Naar <ike@iceland.freeshell.org> - 2012-06-04 07:28 +0000
                Re: GOTOs discovered in "real world" program! (was Re: Error returns and repeated code, GOTOs, etc.) Tim Rentsch <txr@alumni.caltech.edu> - 2012-06-08 01:28 -0700
      Why was there a GOTO? (was Re: GOTOs discovered in "real world" program!) mike3 <mike4ty4@yahoo.com> - 2012-06-06 02:54 -0700
        Re: Why was there a GOTO? (was Re: GOTOs discovered in "real world" program!) Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-06-06 13:14 +0100
          Re: Why was there a GOTO? (was Re: GOTOs discovered in "real world" program!) pete <pfiland@mindspring.com> - 2012-06-06 09:22 -0400
            Re: Why was there a GOTO? (was Re: GOTOs discovered in "real world" program!) Willem <willem@toad.stack.nl> - 2012-06-06 13:49 +0000
              Re: Why was there a GOTO? (was Re: GOTOs discovered in "real world" program!) Rui Maciel <rui.maciel@gmail.com> - 2012-06-06 14:56 +0100
              Re: Why was there a GOTO? (was Re: GOTOs discovered in "real world" program!) James Kuyper <jameskuyper@verizon.net> - 2012-06-06 10:17 -0400
        Re: Why was there a GOTO? (was Re: GOTOs discovered in "real world" program!) Angel <angel+news@spamcop.net> - 2012-06-06 13:06 +0000
          Re: Why was there a GOTO? (was Re: GOTOs discovered in "real world" program!) mike3 <mike4ty4@yahoo.com> - 2012-06-07 16:26 -0700
            Re: Why was there a GOTO? (was Re: GOTOs discovered in "real world" program!) Angel <angel+news@spamcop.net> - 2012-06-08 14:26 +0000
              Re: Why was there a GOTO? (was Re: GOTOs discovered in "real world" program!) mike3 <mike4ty4@yahoo.com> - 2012-06-08 15:19 -0700
                Re: Why was there a GOTO? (was Re: GOTOs discovered in "real world" program!) Angel <angel+news@spamcop.net> - 2012-06-08 23:32 +0000
                Re: Why was there a GOTO? (was Re: GOTOs discovered in "real world" program!) "BartC" <bc@freeuk.com> - 2012-06-09 01:14 +0100
                  Re: Why was there a GOTO? (was Re: GOTOs discovered in "real world" program!) Jean-Christophe <5.d@free.fr> - 2012-06-09 04:36 -0700
                  Re: Why was there a GOTO? (was Re: GOTOs discovered in "real world" program!) Malcolm McLean <malcolm.mclean5@btinternet.com> - 2012-06-09 05:54 -0700
                    Re: Why was there a GOTO? (was Re: GOTOs discovered in "real world" program!) "BartC" <bc@freeuk.com> - 2012-06-09 16:12 +0100
                      Re: Why was there a GOTO? (was Re: GOTOs discovered in "real world" program!) Malcolm McLean <malcolm.mclean5@btinternet.com> - 2012-06-09 08:44 -0700
                        Re: Why was there a GOTO? (was Re: GOTOs discovered in "real world" program!) "io_x" <a@b.c.invalid> - 2012-06-10 18:23 +0200
                          Re: Why was there a GOTO? (was Re: GOTOs discovered in "real world" program!) "io_x" <a@b.c.invalid> - 2012-06-10 19:42 +0200
                            Re: Why was there a GOTO? (was Re: GOTOs discovered in "real world" program!) "io_x" <a@b.c.invalid> - 2012-06-14 18:00 +0200
                          Re: Why was there a GOTO? (was Re: GOTOs discovered in "real world" program!) nick_keighley_nospam@hotmail.com - 2012-06-11 01:14 -0700
                            Re: Why was there a GOTO? (was Re: GOTOs discovered in "real world" program!) "io_x" <a@b.c.invalid> - 2012-06-12 08:01 +0200
                              Re: Why was there a GOTO? (was Re: GOTOs discovered in "real world" program!) nick_keighley_nospam@hotmail.com - 2012-06-12 05:27 -0700
                            Re: Why was there a GOTO? (was Re: GOTOs discovered in "real world" program!) "io_x" <a@b.c.invalid> - 2012-06-12 08:44 +0200
                    Re: Why was there a GOTO? (was Re: GOTOs discovered in "real world" program!) jgk@panix.com (Joe keane) - 2012-06-11 17:24 +0000
                      Re: Why was there a GOTO? (was Re: GOTOs discovered in "real world" program!) "BartC" <bc@freeuk.com> - 2012-06-11 19:02 +0100
                        Re: Why was there a GOTO? (was Re: GOTOs discovered in "real world" program!) jgk@panix.com (Joe keane) - 2012-06-12 20:03 +0000
                          Re: Why was there a GOTO? (was Re: GOTOs discovered in "real world" program!) nick_keighley_nospam@hotmail.com - 2012-06-13 00:58 -0700
                            Re: Why was there a GOTO? (was Re: GOTOs discovered in "real world" program!) James Kuyper <jameskuyper@verizon.net> - 2012-06-13 06:37 -0400
                              Evil in Programming   Was: Why was there a GOTO? (was Re: GOTOs discovered in "real world" program!) nick_keighley_nospam@hotmail.com - 2012-06-13 04:50 -0700
                            Re: Why was there a GOTO? (was Re: GOTOs discovered in "real world" program!) gwowen <gwowen@gmail.com> - 2012-06-13 07:02 -0700
                            Re: Why was there a GOTO? (was Re: GOTOs discovered in "real world" program!) Les Cargill <lcargill99@comcast.com> - 2012-06-13 12:47 -0500
                              Re: Why was there a GOTO? (was Re: GOTOs discovered in "real world" program!) James Kuyper <jameskuyper@verizon.net> - 2012-06-13 14:17 -0400
                                Re: Why was there a GOTO? (was Re: GOTOs discovered in "real world" program!) Willem <willem@toad.stack.nl> - 2012-06-13 19:44 +0000
                                  Re: Why was there a GOTO? (was Re: GOTOs discovered in "real world" program!) James Kuyper <jameskuyper@verizon.net> - 2012-06-13 15:52 -0400
                                Re: Why was there a GOTO? (was Re: GOTOs discovered in "real world" program!) Les Cargill <lcargill99@comcast.com> - 2012-06-13 17:45 -0500
                                  Re: Why was there a GOTO? (was Re: GOTOs discovered in "real world" program!) James Kuyper <jameskuyper@verizon.net> - 2012-06-13 19:07 -0400
                                Re: Why was there a GOTO? (was Re: GOTOs discovered in "real world" program!) jgk@panix.com (Joe keane) - 2012-06-14 23:03 +0000
                                Re: Why was there a GOTO? (was Re: GOTOs discovered in "real world" program!) Kaz Kylheku <kaz@kylheku.com> - 2012-06-14 23:25 +0000
                                  Re: Why was there a GOTO? (was Re: GOTOs discovered in "real world" program!) Les Cargill <lcargill99@comcast.com> - 2012-06-14 19:25 -0500
                                    Re: Why was there a GOTO? (was Re: GOTOs discovered in "real world" program!) 88888 Dihedral <dihedral88888@googlemail.com> - 2012-06-17 20:32 -0700
                                      Re: Why was there a GOTO? (was Re: GOTOs discovered in "real world" program!) nick_keighley_nospam@hotmail.com - 2012-06-18 01:30 -0700
                                        Re: Why was there a GOTO? (was Re: GOTOs discovered in "real world" program!) Les Cargill <lcargill99@comcast.com> - 2012-06-18 07:06 -0500
                            Re: Why was there a GOTO? Gareth Owen <gwowen@gmail.com> - 2012-06-13 19:57 +0100
                  Re: Why was there a GOTO? (was Re: GOTOs discovered in "real world" program!) jgk@panix.com (Joe keane) - 2012-06-10 16:56 +0000
    Another problem (was Re: Error returns and repeated code, GOTOs, etc.) mike3 <mike4ty4@yahoo.com> - 2012-05-31 16:46 -0700
      Re: Another problem (was Re: Error returns and repeated code, GOTOs, etc.) Ian Collins <ian-news@hotmail.com> - 2012-06-01 15:55 +1200
    Re: Error returns and repeated code, GOTOs, etc. gwowen <gwowen@gmail.com> - 2012-06-06 08:12 -0700

Page 2 of 7 — ← Prev page 1 [2] 3 4 5 6 7  Next page →


#21704

FromMartin Shobe <martin.shobe@yahoo.com>
Date2012-06-02 19:27 +0000
Message-ID<jqdpfq$pqs$1@dont-email.me>
In reply to#21677
Joe keane wrote:

> In article <slrnjscii7.2aju.willem@toad.stack.nl>,
> Willem  <willem@toad.stack.nl> wrote:
>>I agree gotos are better.
>
> I agree gotos are better.
>
> int func(int x)
> {
>     int err;
>     struct bar *a;
>     struct bar *b;
>     struct bar *c;
>     int err2;
>
>     err = allocate_bar(&a);
>     if (err != X_OK)
> 	goto done;
>
>     err = allocate_bar(&b);
>     if (err != X_OK)
> 	goto free_a;
>
>     err = allocate_bar(&c);
>     if (err != X_OK)
> 	goto free_b;
>
>     ...
>
>     /* normal exit */
>
>     err = free_bar(c);
>
>     err2 = free_bar(b);
>     err = err != X_OK ? err : err2;
>
>     err2 = free_bar(a);
>     err = err != X_OK ? err : err2;
>     return err;
>
>     /* error exit */
>
>     err2 = free_bar(c);
>     (void) err2;
>
>   free_b:
>     err2 = free_bar(b);
>     (void) err2;
>
>   free_a:
>     err2 = free_bar(a);
>     (void) err2;
>
>   done:
>     return err;
> }
>
> do that in C++!

If you insist. :)

void func(int x)
{
	std::unique_ptr<bar> a = new bar();
	std::unique_ptr<bar> b = new bar();
	std::unique_ptr<bar> c = new bar();

	...
}

Martin Shobe

[toc] | [prev] | [next] | [standalone]


#21784

Fromjgk@panix.com (Joe keane)
Date2012-06-03 23:48 +0000
Message-ID<jqgt42$osf$1@reader1.panix.com>
In reply to#21704
In article <jqdpfq$pqs$1@dont-email.me>,
Martin Shobe  <martin.shobe@yahoo.com> wrote:
>If you insist. :)
>
>void func(int x)
>{
>	std::unique_ptr<bar> a = new bar();
>	std::unique_ptr<bar> b = new bar();
>	std::unique_ptr<bar> c = new bar();
>
>	...
>}

Does it return the correct error code if an error occurs in the
unallocate function?

[toc] | [prev] | [next] | [standalone]


#21786

FromIan Collins <ian-news@hotmail.com>
Date2012-06-04 11:53 +1200
Message-ID<a32bonF5maU7@mid.individual.net>
In reply to#21784
On 06/ 4/12 11:48 AM, Joe keane wrote:
> In article<jqdpfq$pqs$1@dont-email.me>,
> Martin Shobe<martin.shobe@yahoo.com>  wrote:
>> If you insist. :)
>>
>> void func(int x)
>> {
>> 	std::unique_ptr<bar>  a = new bar();
>> 	std::unique_ptr<bar>  b = new bar();
>> 	std::unique_ptr<bar>  c = new bar();
>>
>> 	...
>> }
>
> Does it return the correct error code if an error occurs in the
> unallocate function?

delete does not return errors and func is declared void.

Your code ignored the errors from free_bar (in a very ugly fashion I 
might add).

-- 
Ian Collins

[toc] | [prev] | [next] | [standalone]


#21793

FromMartin Shobe <martin.shobe@yahoo.com>
Date2012-06-04 03:32 +0000
Message-ID<jqha7r$a5i$1@dont-email.me>
In reply to#21784
Joe keane wrote:

> In article <jqdpfq$pqs$1@dont-email.me>,
> Martin Shobe  <martin.shobe@yahoo.com> wrote:
>>If you insist. :)
>>
>>void func(int x)
>>{
>>	std::unique_ptr<bar> a = new bar();
>>	std::unique_ptr<bar> b = new bar();
>>	std::unique_ptr<bar> c = new bar();
>>
>>	...
>>}
>
> Does it return the correct error code if an error occurs in the
> unallocate function?

Return it? No. But it would throw the correct exception.

Martin Shobe

[toc] | [prev] | [next] | [standalone]


#21545

Fromnick_keighley_nospam@hotmail.com
Date2012-05-30 07:25 -0700
Message-ID<0df0a4ca-7bd7-4ac5-a932-53b055e74469@googlegroups.com>
In reply to#21528
On Wednesday, May 30, 2012 9:55:55 AM UTC+1, MarkBluemel wrote:
> On 05/30/2012 06:44 AM, mike3 wrote:
> 
> > But we've violated Djikstra and we've used a "bad" goto. How would
> > _you_ write MyFunc() above?
> 
> I personally don't like the infinitely indented if approach...

nor me. Even goto is cleaner.

[toc] | [prev] | [next] | [standalone]


#21857

Fromfalk@rahul.net (Edward A. Falk)
Date2012-06-05 07:20 +0000
Message-ID<jqkc0k$qe4$2@blue-new.rahul.net>
In reply to#21528
In article <jq4nar$lvm$1@dont-email.me>,
MarkBluemel  <mark_bluemel@pobox.com> wrote:
>On 05/30/2012 06:44 AM, mike3 wrote:
>
>ErrorCode MyFunc()
>{
>           BigNum a = BIGNUM_NULL;
>           BigNum b = BIGNUM_NULL;
>           BigNum c = BIGNUM_NULL;
>           BigNum d = BIGNUM_NULL;
>           ErrorCode rv;
>
>           rv = BigNum_Initialize(&a);
>           if(rv != ERROR_SUCCESS)
>            goto failed;
>
>           ...
>
>failed: /* we'll try and clean up, but won't care too much if it fails */
>         if (d != BIGNUM_NULL)
>           BigNum_Free(&d);
>         ...

This is the style I tend to prefer. I've written a lot of device drivers
in the past, and the attach function generally has to catch about a
dozen failure cases. This is pretty much the cleanest & safest way
to deal with it.

-- 
	-Ed Falk, falk@despams.r.us.com
	http://thespamdiaries.blogspot.com/

[toc] | [prev] | [next] | [standalone]


#21530

FromIan Collins <ian-news@hotmail.com>
Date2012-05-30 21:50 +1200
Message-ID<a2m8qiFad7U10@mid.individual.net>
In reply to#21522
On 05/30/12 05:44 PM, mike3 wrote:
> Hi.
>
> I've got some stuff that looks like this:
>
> ---
> ErrorCode MyFunc()
> {
>            BigNum a, b, c, d;
>            ErrorCode rv;
>
>            rv = BigNum_Initialize(&a);
>            if(rv != ERROR_SUCCESS)
>              return(rv);
>
>            rv = BigNum_Initialize(&b);
>            if(rv != ERROR_SUCCESS)
>            {
>              BigNum_Free(&a); /* and what if THIS fails??? :) */
>              return(rv);
>            }

<snip lots more of the same>

I know I'm inviting flames, but this really is a classic case where C++ 
if you have access to it can be used as a better C....

-- 
Ian Collins

[toc] | [prev] | [next] | [standalone]


#21531

FromMalcolm McLean <malcolm.mclean5@btinternet.com>
Date2012-05-30 03:34 -0700
Message-ID<1159b302-6dba-4642-bfc3-f5fd90f73f0c@googlegroups.com>
In reply to#21530
בתאריך יום רביעי, 30 במאי 2012 10:50:10 UTC+1, מאת Ian Collins:
> On 05/30/12 05:44 PM, mike3 wrote:
> > Hi.
> >
> > I've got some stuff that looks like this:
> >
> > ---
> > ErrorCode MyFunc()
> > {
> >            BigNum a, b, c, d;
> >            ErrorCode rv;
> >
> >            rv = BigNum_Initialize(&a);
> >            if(rv != ERROR_SUCCESS)
> >              return(rv);
> >
> >            rv = BigNum_Initialize(&b);
> >            if(rv != ERROR_SUCCESS)
> >            {
> >              BigNum_Free(&a); /* and what if THIS fails??? :) */
> >              return(rv);
> >            }
>
> I know I'm inviting flames, but this really is a classic case where C++ 
> if you have access to it can be used as a better C....
> 
You want

rv = BigNum_Initilaize(&a);
if(rv)
  goto error_exit;
rv = BigNum_Add(&a, &b, &c);
if(!rv)
   goto error_exit;

it's slightly messier than exceptions, but unlike exceptions it's easier to test and harder to abuse.
You don't want to be defining the symbol,ERROR_SUCCESS in a bignum.h header, and you don't want bignum.c dependent on myerrorcodes.h. However -1 for out of memory is pretty standard.

[toc] | [prev] | [next] | [standalone]


#21532

FromIan Collins <ian-news@hotmail.com>
Date2012-05-30 22:57 +1200
Message-ID<a2mco0Fad7U12@mid.individual.net>
In reply to#21531
On 05/30/12 10:34 PM, Malcolm McLean wrote:
> בתאריך יום רביעי, 30 במאי 2012 10:50:10 UTC+1, מאת Ian Collins:
>> On 05/30/12 05:44 PM, mike3 wrote:
>>> Hi.
>>>
>>> I've got some stuff that looks like this:
>>>
>>> ---
>>> ErrorCode MyFunc()
>>> {
>>>             BigNum a, b, c, d;
>>>             ErrorCode rv;
>>>
>>>             rv = BigNum_Initialize(&a);
>>>             if(rv != ERROR_SUCCESS)
>>>               return(rv);
>>>
>>>             rv = BigNum_Initialize(&b);
>>>             if(rv != ERROR_SUCCESS)
>>>             {
>>>               BigNum_Free(&a); /* and what if THIS fails??? :) */
>>>               return(rv);
>>>             }
>>
>> I know I'm inviting flames, but this really is a classic case where C++
>> if you have access to it can be used as a better C....
>>
> You want
>
> rv = BigNum_Initilaize(&a);
> if(rv)
>    goto error_exit;
> rv = BigNum_Add(&a,&b,&c);
> if(!rv)
>     goto error_exit;
>
> it's slightly messier than exceptions, but unlike exceptions it's easier to test and harder to abuse.

It's way uglier and no easier to test.

> You don't want to be defining the symbol,ERROR_SUCCESS in a bignum.h header, and you don't want bignum.c dependent on myerrorcodes.h. However -1 for out of memory is pretty standard.

I'm not sure what you are saying there.

-- 
Ian Collins

[toc] | [prev] | [next] | [standalone]


#21533

FromWillem <willem@toad.stack.nl>
Date2012-05-30 11:04 +0000
Message-ID<slrnjsbvmg.2vbf.willem@toad.stack.nl>
In reply to#21531
Malcolm McLean wrote:
) You want
)
) rv = BigNum_Initilaize(&a);
) if(rv)
)   goto error_exit;
) rv = BigNum_Add(&a, &b, &c);
) if(!rv)
)    goto error_exit;

Why one positive and one negative test?  Is that a typo?


SaSW, Willem
-- 
Disclaimer: I am in no way responsible for any of the statements
            made in the above text. For all I know I might be
            drugged or something..
            No I'm not paranoid. You all think I'm paranoid, don't you !
#EOT

[toc] | [prev] | [next] | [standalone]


#21535

Fromnick_keighley_nospam@hotmail.com
Date2012-05-30 04:53 -0700
Message-ID<f82b909a-10c5-4b09-a3a5-5df05696d1ff@googlegroups.com>
In reply to#21531
On Wednesday, May 30, 2012 11:34:13 AM UTC+1, Malcolm McLean wrote:
> בתאריך יום רביעי, 30 במאי 2012 10:50:10 UTC+1, מאת Ian Collins:
> > On 05/30/12 05:44 PM, mike3 wrote:

> > > I've got some stuff that looks like this:
> > >
> > > ---
> > > ErrorCode MyFunc()
> > > {
> > >            BigNum a, b, c, d;
> > >            ErrorCode rv;
> > >
> > >            rv = BigNum_Initialize(&a);
> > >            if(rv != ERROR_SUCCESS)
> > >              return(rv);
> > >
> > >            rv = BigNum_Initialize(&b);
> > >            if(rv != ERROR_SUCCESS)
> > >            {
> > >              BigNum_Free(&a); /* and what if THIS fails??? :) */
> > >              return(rv);
> > >            }
> >
> > I know I'm inviting flames, but this really is a classic case where C++ 
> > if you have access to it can be used as a better C....

I was tempted to say the same. Exceptiosn and RAII should be able to make it quite tidy.

> You want
> 
> rv = BigNum_Initilaize(&a);
> if(rv)
>   goto error_exit;
> rv = BigNum_Add(&a, &b, &c);
> if(!rv)
>    goto error_exit;
> 
> it's slightly messier than exceptions, but unlike exceptions it's easier to test and harder to abuse.

why?

> You don't want to be defining the symbol,ERROR_SUCCESS in a bignum.h header,

why? It's part of the Bignum interface.

> and you don't want bignum.c dependent on myerrorcodes.h. 

ditto

> However -1 for out of memory is pretty standard.

I'd rather use a named constant... (I only recently found out this was a controversial opinion!)

[toc] | [prev] | [next] | [standalone]


#21538

From"BartC" <bc@freeuk.com>
Date2012-05-30 13:28 +0100
Message-ID<jq53t7$q76$1@dont-email.me>
In reply to#21522
"mike3" <mike4ty4@yahoo.com> wrote in message
news:7e54cf3c-d514-407b-a9b6-c0009589d242@ra8g2000pbc.googlegroups.com...

> ErrorCode MyFunc()
> {
>          BigNum a, b, c, d;
>          ErrorCode rv;
>
>          rv = BigNum_Initialize(&a);
>          if(rv != ERROR_SUCCESS)
>            return(rv);

<etc>

> But this is nasty, with all those duplicated "free" blocks. What can
> be done to relieve this code duplication? It's ugly, it hurts my
> freaking eyes and my nose is screaming for relief. And it hurts
> maintainability to no end -- what if we decide to add more capability
> to MyFunc() that requires additional bignums? Oy... We'd have to
> update _all those blocks_.

I think you've designed in too much error-checking, and made your bignums
unwieldy to initialise and manage.

Any code trying to actually do something with bignums would be lost amongst
all the error-checking and recovery. And if there was some conditional code,
and every single calculation needed checking, then you would easily lose
track of what needed freeing and what didn't.

Why does an initialisation need error-checking anyway? Does it involve
allocating memory?

It's not clear if a bignum is a pointer or a struct; they could be simply
initialised to NULL or to {0}, leaving it to the bignum arithmetic routines
to allocate as needed, and do the error-checking. Your code might still need
to free, but the free routine can leave alone bignums that are still NULL or
{0}.

Actually, for memory management, you should take seriously the idea put
forward of using garbage collection. Then you don't need to explicitly free
memory.

> ErrorCode MyFunc()
> {
>          BigNum a, b, c, d;
>          ErrorCode rv;
>
>          rv = BigNum_Initialize(&a); if(rv != ERROR_SUCCESS) goto
> fail0;
>          rv = BigNum_Initialize(&b); if(rv != ERROR_SUCCESS) goto
> fail1;
>          rv = BigNum_Initialize(&c); if(rv != ERROR_SUCCESS) goto
> fail2;
>          rv = BigNum_Initialize(&d); if(rv != ERROR_SUCCESS) goto
> fail3;

(As a personal preference, I would use something like BN_Init(). Then these
big names would dominate the code so much.)

And, whatever the initialisation does, is it really necessary to check the
result? Why not leave it to routines such as BigNum_Set() to validate it's
arguments; if an argument has not been properly initialised, then *it*
returns a failure code.

>
>          /* do some math */
>          rv = BigNum_Set(&b, 3); if(rv != ERROR_SUCCESS) goto fail4;
>
>          ...
>
>          /* free buffers */
>          rv = BigNum_Free(&d); if(rv != ERROR_SUCCESS) goto fail3;

And what is likely to go wrong with BigNum_Free() that will need checking?
Will the caller of this function care, provided it gets the right answer? Or
is the result of BigNum_Set() likely to be invalidated if a subsequent free
fails?

>          rv = BigNum_Free(&c); if(rv != ERROR_SUCCESS) goto fail2;
>          rv = BigNum_Free(&b); if(rv != ERROR_SUCCESS) goto fail1;
>          rv = BigNum_Free(&a); if(rv != ERROR_SUCCESS) goto fail0;
>
>          return(ERROR_SUCCESS);
>
> fail4:    BigNum_Free(&d);
> fail3:    BigNum_Free(&c);
> fail2:    BigNum_Free(&b);
> fail1:    BigNum_Free(&a);
> fail0:    return(rv);
> }

If you do need to do this, then just combine all the statuses of multiple
calls to BigNum_Free() (the error code needs to be of a format that will
allow | or & operations).

I know your post is about managing error recovery, rather than the merits of
a particular library. But, someone needing to do arithmetic via function
calls, might prefer to keep error-checking low-key, perhaps something like
this:

ErrorCode BigNum_Average(BigNum A, BigNum B, BigNum Result) {
BigNum Two=NULL;
ErrorCode e=0;

 e |= BigNum_Add(A,B, Result);
 e |= BigNum_Set(&Two, 2);
 e |= BigNum_Div(Result, Two, Result);

 e |= BigNum_Free(&Two);

 return e;
}

Then it is still reasonably easy to follow what's going on. (Actually, I 
wouldn't bother with the error codes myself; I would build a status into the 
bignum itself, a bit like the Nans of floating point.)

-- 
Bartc 

[toc] | [prev] | [next] | [standalone]


#21558

Frommike3 <mike4ty4@yahoo.com>
Date2012-05-30 13:57 -0700
Message-ID<02a2c453-eab6-4df3-9636-eff376dd3f58@hy7g2000pbc.googlegroups.com>
In reply to#21538
On May 30, 6:28 am, "BartC" <b...@freeuk.com> wrote:
> "mike3" <mike4...@yahoo.com> wrote in message
<snip>
> I think you've designed in too much error-checking, and made your bignums
> unwieldy to initialise and manage.
>
> Any code trying to actually do something with bignums would be lost amongst
> all the error-checking and recovery. And if there was some conditional code,
> and every single calculation needed checking, then you would easily lose
> track of what needed freeing and what didn't.
>
> Why does an initialisation need error-checking anyway? Does it involve
> allocating memory?
>

Yes. You have to allocate memory to initialize (for the buffer holding
the
bignum's digits.). And that can fail. Thus, I return an error code.

> It's not clear if a bignum is a pointer or a struct; they could be simply
> initialised to NULL or to {0}, leaving it to the bignum arithmetic routines
> to allocate as needed, and do the error-checking. Your code might still need
> to free, but the free routine can leave alone bignums that are still NULL or
> {0}.
>
> Actually, for memory management, you should take seriously the idea put
> forward of using garbage collection. Then you don't need to explicitly free
> memory.
>
> > ErrorCode MyFunc()
> > {
> >          BigNum a, b, c, d;
> >          ErrorCode rv;
>
> >          rv = BigNum_Initialize(&a); if(rv != ERROR_SUCCESS) goto
> > fail0;
> >          rv = BigNum_Initialize(&b); if(rv != ERROR_SUCCESS) goto
> > fail1;
> >          rv = BigNum_Initialize(&c); if(rv != ERROR_SUCCESS) goto
> > fail2;
> >          rv = BigNum_Initialize(&d); if(rv != ERROR_SUCCESS) goto
> > fail3;
>
> (As a personal preference, I would use something like BN_Init(). Then these
> big names would dominate the code so much.)
>
> And, whatever the initialisation does, is it really necessary to check the
> result? Why not leave it to routines such as BigNum_Set() to validate it's
> arguments; if an argument has not been properly initialised, then *it*
> returns a failure code.
>

What do you do with that failure code? :) See, you still need a
handler around
every math routine call. This would help with the Init() functions'
handlers, though.

>
>
> >          /* do some math */
> >          rv = BigNum_Set(&b, 3); if(rv != ERROR_SUCCESS) goto fail4;
>
> >          ...
>
> >          /* free buffers */
> >          rv = BigNum_Free(&d); if(rv != ERROR_SUCCESS) goto fail3;
>
> And what is likely to go wrong with BigNum_Free() that will need checking?
> Will the caller of this function care, provided it gets the right answer? Or
> is the result of BigNum_Set() likely to be invalidated if a subsequent free
> fails?
>

Looking at the free function, it can return one error code:
ERROR_INVALID_STORAGE_TYPE. A bignum can be stored on either
memory or on disk (disk not yet implemented, but the stuff is there to
allow
for its implementation). There is, of course, a field in the bignum
that indicates
which is being used. If this is something weird -- something other
than
"memory" or "disk", then the "Free" function would get confused as it
wouldn't know how to properly free the bignum. Normally, that
shouldn't
happen. But if someone was doing something silly, the catch is there
to catch that. So, should I just remove that error return and say,
"well,
if you're messing around with this in some improper fashion, then
expect things to break"? But then we run into the problem of where
the field is corrupted due to a memory bug. In this case, a fail could
be useful (although one of the other bignum routines could puke as
well.).

An idea I had is to drop the type field altogether and use
something like this:

typedef struct BigNum {
...
     Digit *RAMBuffer;
     FILE *DiskBuffer;
...
} BigNum;

and when BigNum is initialized to RAM storage, RAMBuffer holds a ptr,
and DiskBuffer is NULL, and when BigNum is initialized to Disk
storage,
RAMBuffer is NULL, and DiskBuffer holds a ptr. Then there's no field
that
can get an invalid value, and if someone tries to set the NULL ptrs
to something else, then expect things to break. (If a ptr gets
corrupted,
you're pretty screwed anyway and there isn't much you can do about it)

> >          rv = BigNum_Free(&c); if(rv != ERROR_SUCCESS) goto fail2;
> >          rv = BigNum_Free(&b); if(rv != ERROR_SUCCESS) goto fail1;
> >          rv = BigNum_Free(&a); if(rv != ERROR_SUCCESS) goto fail0;
>
> >          return(ERROR_SUCCESS);
>
> > fail4:    BigNum_Free(&d);
> > fail3:    BigNum_Free(&c);
> > fail2:    BigNum_Free(&b);
> > fail1:    BigNum_Free(&a);
> > fail0:    return(rv);
> > }
>
> If you do need to do this, then just combine all the statuses of multiple
> calls to BigNum_Free() (the error code needs to be of a format that will
> allow | or & operations).
>

The thing here is, that I have a general "universal" list of error
codes, numbered
0 (= success), 1, 2, 3, etc. and functions take their return codes
from that.
Perhaps that's not the best way to do it? (I was inspired by
Microsoft's Windows
system with all its error codes.) E.g. we could use for the bignum
routines
a special set of 32-bit (or even 64-bit, in this case -- this program
is for 64-bit
systems anyway) error codes with at most 32 possible errors (probably
way more than'd be needed from a simple bignum routine!) error codes
that
indicate errors by bit flags (all zeroes = success), and could be ORed
together
to get what errors occurred in the code under consideration.

> I know your post is about managing error recovery, rather than the merits of
> a particular library. But, someone needing to do arithmetic via function
> calls, might prefer to keep error-checking low-key, perhaps something like
> this:
>
> ErrorCode BigNum_Average(BigNum A, BigNum B, BigNum Result) {
> BigNum Two=NULL;
> ErrorCode e=0;
>
>  e |= BigNum_Add(A,B, Result);
>  e |= BigNum_Set(&Two, 2);
>  e |= BigNum_Div(Result, Two, Result);
>
>  e |= BigNum_Free(&Two);
>
>  return e;
>
> }
>
> Then it is still reasonably easy to follow what's going on. (Actually, I
> wouldn't bother with the error codes myself; I would build a status into the
> bignum itself, a bit like the Nans of floating point.)
>

Yes, a "NAN" or error flag is another possibility. Flag would be best
-- it's
real easy to check and you could set multiple values -- like 1 for bad
alloc,
2 for overflow, etc.

[toc] | [prev] | [next] | [standalone]


#21542

From"io_x" <a@b.c.invalid>
Date2012-05-30 15:19 +0200
Message-ID<4fc61d23$0$1385$4fafbaef@reader2.news.tin.it>
In reply to#21522
"mike3" <mike4ty4@yahoo.com> ha scritto nel messaggio
news:7e54cf3c-d514-407b-a9b6-c0009589d242@ra8g2000pbc.googlegroups.com...
> Hi.
>
> I've got some stuff that looks like this:

this is my way...

ErrorCode MyFunc(void)
{BigNum a, b, c, d;
 ErrorCode  rv, rk;

 if((rv=BigNum_Initialize(&a))!= ERROR_SUCCESS)
                             return  rv;
 if((rv = BigNum_Initialize(&b)) != ERROR_SUCCESS)
    {ex1:   BigNum_Free(&a); return  rv;}
 if((rv = BigNum_Initialize(&c)) != ERROR_SUCCESS)
    {ex2:   BigNum_Free(&b); goto   ex1;}
 if((rv = BigNum_Initialize(&d)) != ERROR_SUCCESS)
    {ex3:   BigNum_Free(&c); goto   ex2;}
 if((rv = BigNum_Set(&b, 3))     != ERROR_SUCCESS)
    {ex4:   BigNum_Free(&d); goto   ex3;}
          ...
          /* free buffers */
 rv=ERROR_SUCCESS;
 if((rk=BigNum_Free(&d))!=ERROR_SUCCESS)  rv=rk;
 if((rk=BigNum_Free(&c))!=ERROR_SUCCESS)  rv=rk;
 if((rk=BigNum_Free(&b))!=ERROR_SUCCESS)  rv=rk;
 if((rk=BigNum_Free(&a))!=ERROR_SUCCESS)  rv=rk;
 return rv;
}


> But this is nasty, with all those duplicated "free" blocks. What can
> be done to relieve this code duplication? It's ugly, it hurts my
> freaking eyes and my nose is screaming for relief. And it hurts
> maintainability to no end -- what if we decide to add more capability
> to MyFunc() that requires additional bignums? Oy... We'd have to
> update _all those blocks_.
>
> I was thinking about using a "goto", but Gotos are Bad, aren't they? I
> can't believe I may have to use a goto! What about Djikstra's famous
> letter? Is it possible that perhaps a goto may be GOOD here? Because
> with a goto this would all seem much simpler. If we violate Djikstra,
> we get:
>
> ---
>
> ErrorCode MyFunc()
> {
>          BigNum a, b, c, d;
>          ErrorCode rv;
>
>          rv = BigNum_Initialize(&a); if(rv != ERROR_SUCCESS) goto
> fail0;
>          rv = BigNum_Initialize(&b); if(rv != ERROR_SUCCESS) goto
> fail1;
>          rv = BigNum_Initialize(&c); if(rv != ERROR_SUCCESS) goto
> fail2;
>          rv = BigNum_Initialize(&d); if(rv != ERROR_SUCCESS) goto
> fail3;
>
>          /* do some math */
>          rv = BigNum_Set(&b, 3); if(rv != ERROR_SUCCESS) goto fail4;
>
>          ...
>
>          /* free buffers */
>          rv = BigNum_Free(&d); if(rv != ERROR_SUCCESS) goto fail3;

if above fail you not free c, b and a...

>          rv = BigNum_Free(&c); if(rv != ERROR_SUCCESS) goto fail2;
>          rv = BigNum_Free(&b); if(rv != ERROR_SUCCESS) goto fail1;
>          rv = BigNum_Free(&a); if(rv != ERROR_SUCCESS) goto fail0;
>
>          return(ERROR_SUCCESS);
>
> fail4:    BigNum_Free(&d);
> fail3:    BigNum_Free(&c);
> fail2:    BigNum_Free(&b);
> fail1:    BigNum_Free(&a);
> fail0:    return(rv);
> }


[toc] | [prev] | [next] | [standalone]


#21570

FromMark Bluemel <mark.bluemel@googlemail.com>
Date2012-05-31 01:10 -0700
Message-ID<c2f85360-c7d9-4056-bf1d-0c75a6b93d11@d17g2000vbv.googlegroups.com>
In reply to#21542
On May 30, 2:19 pm, "io_x" <a...@b.c.invalid> wrote:

> this is my way...

Why does this not surprise me?

> ErrorCode MyFunc(void)
> {BigNum a, b, c, d;
>  ErrorCode  rv, rk;
>
>  if((rv=BigNum_Initialize(&a))!= ERROR_SUCCESS)
>                              return  rv;
>  if((rv = BigNum_Initialize(&b)) != ERROR_SUCCESS)
>     {ex1:   BigNum_Free(&a); return  rv;}
>  if((rv = BigNum_Initialize(&c)) != ERROR_SUCCESS)
>     {ex2:   BigNum_Free(&b); goto   ex1;}
>  if((rv = BigNum_Initialize(&d)) != ERROR_SUCCESS)
>     {ex3:   BigNum_Free(&c); goto   ex2;}
>  if((rv = BigNum_Set(&b, 3))     != ERROR_SUCCESS)
>     {ex4:   BigNum_Free(&d); goto   ex3;}

Ouch!

[toc] | [prev] | [next] | [standalone]


#21691

From"io_x" <a@b.c.invalid>
Date2012-06-02 17:34 +0200
Message-ID<4fca3222$0$1380$4fafbaef@reader2.news.tin.it>
In reply to#21542
"io_x" <a@b.c.invalid> ha scritto nel messaggio
news:4fc61d23$0$1385$4fafbaef@reader2.news.tin.it...
>
> "mike3" <mike4ty4@yahoo.com> ha scritto nel messaggio
> news:7e54cf3c-d514-407b-a9b6-c0009589d242@ra8g2000pbc.googlegroups.com...
>> Hi.
>>
>> I've got some stuff that looks like this:
>
> this is my way...

this could be my way...
probabily i use not well C macro...
'2' exit[it is always one]: one for error one for ok

#include <stdio.h>

#define  u32   unsigned
#define  u64   unsigned __int64
#define  i32   int
#define  u16   unsigned short
#define  u8    unsigned char
#define  i8    char
#define  i16   short
#define  long_long   __int64

#define  sdC  __stdcall
#define  ooo  cout
#define  S    sizeof
#define  MM   Malloc_m
#define  FF   Free_m
#define  F   for
#define  R   return
#define  W   while
#define  G   goto
#define  P   printf
#define  jj(x) if((x)==-1)  goto

typedef u32*  BigNum;

// dummy functions
i32  BigNum_Initialize(BigNum*  a)          {R (i32)(*a=0);}
i32  BigNum_Set(BigNum* a, u32  b)          {R (i32)(*a=(u32*)b);}
i32  BigNum_Add(BigNum* c,BigNum*  b, u32 r){R (i32)(*c=(u32*)((u32)*b+r));}
i32  BigNum_Add(BigNum* c,BigNum*  b, BigNum* r)
{R (i32)(*c=(u32*)((u32)*b+(u32)*r));}
i32  BigNum_Free(BigNum* a)                 {R (i32)(*a=0);}

i32 MyFunc(void)
{BigNum   a, b, c, d;

 P("Inizio a=%u b=%u c=%u d=%u\n", a, b, c, d);
 if(BigNum_Initialize(&a)== -1)
    {ex0: R  -1;}
 if(BigNum_Initialize(&b)== -1)
    {ex1:   BigNum_Free(&a); G  ex0;}
 if(BigNum_Initialize(&c)== -1)
    {ex2:   BigNum_Free(&b); G  ex1;}
 if(BigNum_Initialize(&d)== -1)
    {ex3:   BigNum_Free(&c); G  ex2;}

 P("Inizilizzati a=%u b=%u c=%u d=%u\n", a, b, c, d);

 if(BigNum_Set(&b, 3)==-1)
    {ex4:   BigNum_Free(&d); G  ex3;}

 jj(BigNum_Add(&c, &b, 3)) ex4;
 jj(BigNum_Add(&a, &d, 3)) ex4;
 jj(BigNum_Add(&d, &c,&a)) ex4;
// etc
// ....
          /* free buffers */
 P("fine a=%u b=%u c=%u d=%u\n", a, b, c, d);

 jj(BigNum_Free(&d))  ex3;
 jj(BigNum_Free(&c))  ex2;
 jj(BigNum_Free(&b))  ex1;
 jj(BigNum_Free(&a))  ex0;

 P("fine1 a=%u b=%u c=%u d=%u\n", a, b, c, d);
 R   0;
}

main(void){R MyFunc();}


[toc] | [prev] | [next] | [standalone]


#21741

From"io_x" <a@b.c.invalid>
Date2012-06-03 10:33 +0200
Message-ID<4fcb2257$0$1375$4fafbaef@reader2.news.tin.it>
In reply to#21691
"io_x" <a@b.c.invalid> ha scritto nel messaggio
news:4fca3222$0$1380$4fafbaef@reader2.news.tin.it...
> #include <stdio.h>
>
> #define  u32   unsigned
> #define  u64   unsigned __int64
> #define  i32   int
> #define  u16   unsigned short
> #define  u8    unsigned char
> #define  i8    char
> #define  i16   short
> #define  long_long   __int64
>
> #define  sdC  __stdcall
> #define  ooo  cout
> #define  S    sizeof
> #define  MM   Malloc_m
> #define  FF   Free_m
> #define  F   for
> #define  R   return
> #define  W   while
> #define  G   goto
> #define  P   printf
> #define  jj(x) if((x)==-1)  goto
>
> typedef u32*  BigNum;
>
> // dummy functions
> i32  BigNum_Initialize(BigNum*  a)          {R (i32)(*a=0);}
> i32  BigNum_Set(BigNum* a, u32  b)          {R (i32)(*a=(u32*)b);}
> i32  BigNum_Add(BigNum* c,BigNum*  b, u32 r){R (i32)(*c=(u32*)((u32)*b+r));}
> i32  BigNum_Add(BigNum* c,BigNum*  b, BigNum* r)
> {R (i32)(*c=(u32*)((u32)*b+(u32)*r));}
> i32  BigNum_Free(BigNum* a)                 {R (i32)(*a=0);}
>
> i32 MyFunc(void)
> {BigNum   a, b, c, d;
>
> P("Inizio a=%u b=%u c=%u d=%u\n", a, b, c, d);
> if(BigNum_Initialize(&a)== -1)
>    {ex0: R  -1;}
> if(BigNum_Initialize(&b)== -1)
>    {ex1:   BigNum_Free(&a); G  ex0;}
> if(BigNum_Initialize(&c)== -1)
>    {ex2:   BigNum_Free(&b); G  ex1;}
> if(BigNum_Initialize(&d)== -1)
>    {ex3:   BigNum_Free(&c); G  ex2;}
>
> P("Inizilizzati a=%u b=%u c=%u d=%u\n", a, b, c, d);
>
> if(BigNum_Set(&b, 3)==-1)
>    {ex4:   BigNum_Free(&d); G  ex3;}
>
> jj(BigNum_Add(&c, &b, 3)) ex4;
> jj(BigNum_Add(&a, &d, 3)) ex4;
> jj(BigNum_Add(&d, &c,&a)) ex4;
> // etc
> // ....
>          /* free buffers */
> P("fine a=%u b=%u c=%u d=%u\n", a, b, c, d);
>
> jj(BigNum_Free(&d))  ex3;
> jj(BigNum_Free(&c))  ex2;
> jj(BigNum_Free(&b))  ex1;
> jj(BigNum_Free(&a))  ex0;
>
> P("fine1 a=%u b=%u c=%u d=%u\n", a, b, c, d);
> R   0;
> }
>
> main(void){R MyFunc();}

at end i wuold write something as:
i32 MyFunc(BigNum*  x)
{BigNum   a, b, c, d;
 if(x==0)       R  -1;
 a=0; b=3; c=b+3; *x=c+a+d;
 if(x->err())   R  -1;
 else           R   0;
}

but would be the OT c++...
yes it is intendend all c++ exceptions turn off
but except the one
"write out of mem reserved to that program"




[toc] | [prev] | [next] | [standalone]


#21543

FromIke Naar <ike@iceland.freeshell.org>
Date2012-05-30 13:56 +0000
Message-ID<slrn3vfsjsc9os.1qm.ike@iceland.freeshell.org>
In reply to#21522
On 2012-05-30, mike3 <mike4ty4@yahoo.com> wrote:
> I was thinking about using a "goto", but Gotos are Bad, aren't they? I
> can't believe I may have to use a goto! What about Djikstra's famous
> letter? Is it possible that perhaps a goto may be GOOD here? Because
> with a goto this would all seem much simpler. If we violate Djikstra,
> we get:

The name is Dijkstra, not Djikstra.

[toc] | [prev] | [next] | [standalone]


#21551

FromKulin <remailer@reece.net.au>
Date2012-05-30 17:29 +0000
Message-ID<FFSQBZ8K41060.0618171296@reece.net.au>
In reply to#21543
Ike Naar <ike@iceland.freeshell.org> wrote:

> On 2012-05-30, mike3 <mike4ty4@yahoo.com> wrote:
> > I was thinking about using a "goto", but Gotos are Bad, aren't they? I
> > can't believe I may have to use a goto! What about Djikstra's famous
> > letter? Is it possible that perhaps a goto may be GOOD here? Because
> > with a goto this would all seem much simpler. If we violate Djikstra,
> > we get:
> 
> The name is Dijkstra, not Djikstra.

The name is Dumbsonofabitch, not Dijkstra. And he's dead. Good riddance!


[toc] | [prev] | [next] | [standalone]


#21544

FromImpalerCore <jadill33@gmail.com>
Date2012-05-30 07:13 -0700
Message-ID<beb7c8ee-22ba-441c-b1b3-ac6f52c77ca7@q2g2000vbv.googlegroups.com>
In reply to#21522
On May 30, 1:44 am, mike3 <mike4...@yahoo.com> wrote:
> Hi.
>
> I've got some stuff that looks like this:
>
> ---
> ErrorCode MyFunc()
> {
>           BigNum a, b, c, d;
>           ErrorCode rv;
>
>           rv = BigNum_Initialize(&a);
>           if(rv != ERROR_SUCCESS)
>             return(rv);
>
>           rv = BigNum_Initialize(&b);
>           if(rv != ERROR_SUCCESS)
>           {
>             BigNum_Free(&a); /* and what if THIS fails??? :) */
>             return(rv);
>           }
>
>           rv = BigNum_Initialize(&c);
>           if(rv != ERROR_SUCCESS)
>           {
>             BigNum_Free(&b);
>             BigNum_Free(&a);
>             return(rv);
>           }
>
>           rv = BigNum_Initialize(&d);
>           if(rv != ERROR_SUCCESS)
>           {
>             BigNum_Free(&c);
>             BigNum_Free(&b);
>             BigNum_Free(&a);
>             return(rv);
>           }
>
>           /* do some math */
>           rv = BigNum_Set(&b, 3);
>           if(rv != ERROR_SUCCESS)
>           {
>             BigNum_Free(&d);
>             BigNum_Free(&c);
>             BigNum_Free(&b);
>             BigNum_Free(&a);
>             return(rv);
>           }
>
>           ...
>
>           /* free buffers */
>           rv = BigNum_Free(&d);
>           if(rv != ERROR_SUCCESS)
>           {
>             BigNum_Free(&c);
>             BigNum_Free(&b);
>             BigNum_Free(&a);
>             return(rv);
>           }
>
>           rv = BigNum_Free(&c);
>           if(rv != ERROR_SUCCESS)
>           {
>             BigNum_Free(&b);
>             BigNum_Free(&a);
>             return(rv);
>           }
>
>           rv = BigNum_Free(&b);
>           if(rv != ERROR_SUCCESS)
>           {
>             BigNum_Free(&a);
>             return(rv);
>           }
>
>           rv = BigNum_Free(&a);
>           if(rv != ERROR_SUCCESS)
>             return(rv);
>
>           return(ERROR_SUCCESS);}
>
> ---

ErrorCode MyFunc()
{
          BigNum a, b, c, d;
          ErrorCode rv;

          rv = BigNum_Initialize(&a);
          rv = rv == ERROR_SUCCESS ? BigNum_Initialize(&b) : rv;
          rv = rv == ERROR_SUCCESS ? BigNum_Initialize(&c) : rv;
          rv = rv == ERROR_SUCCESS ? BigNum_Initialize(&d) : rv;

          if (rv != ERROR_SUCCESS)
          {
            BigNum_Free(&d);
            BigNum_Free(&c);
            BigNum_Free(&b);
            BigNum_Free(&a);
          }

          return(rv);
}
\endcode

The main issue with this style is that it assumes that BigNum_Free
operates like 'free' where a NULL pointer is a no-op.  If you can
initialize your BigNum variables to a corresponding "NULL" BigNum
state, and BigNum_Free interprets a "NULL" BigNum as a no-op, you can
duplicate this style.  You may need to add a distinct struct
initializer to your variable declarations if BigNum is not a pointer.

\code
/* Define an initialize macro to create a "NULL" BigNum */
#define BIGNUM_NULL_INIT { 0, NULL, ... }

BigNum a = BIGNUM_NULL_INIT;
BigNum b = BIGNUM_NULL_INIT;
BigNum c = BIGNUM_NULL_INIT;
BigNum d = BIGNUM_NULL_INIT;
\endcode

On a side note, the term ERROR_SUCCESS appears contradictory to me.  I
would prefer something like NO_ERROR or SUCCESS if you had control
over the error definitions.

Best regards,
John D.

[toc] | [prev] | [next] | [standalone]


Page 2 of 7 — ← Prev page 1 [2] 3 4 5 6 7  Next page →

Back to top | Article view | comp.lang.c


csiph-web