goto label

Page: << < ..6 Showing page 6 of 6
Author
NorthGuy
Super Member
  • Total Posts : 5309
  • Reward points : 0
  • Joined: 2014/02/23 14:23:23
  • Location: Northern Canada
  • Status: online
Re: goto label 2018/02/12 17:51:07 (permalink)
+1 (1)
jtemples
if (t) {
  t->buffer = malloc();
  if (t->buffer) memset(t->buffer, ...);
}
Which sends the code down the path of deeply nested "if" statements as the code gets more complex, along with needing a bunch of "if" statements at the end of the code for clean up, which the goto code doesn't need.

 
As the code gets more complex, you don't write all the code as a single function anymore. For example, if you want to build a new "t", you create an allocator
 
type_t* construct_the_t( parameters ...) {
  t->buffer = malloc();
  if (t->buffer) memset(t->buffer, ...);
 
  if (t && t->buffer) return t;
 
  destruct_the_t(t);
  return NULL;
}

 
and dellocator
 
destruct_the_t(type_t* t) {
  if (!t) return;
  if (t->buffer) free(t->buffer);
  free(t);
}

 
Then your code becomes:
 
t = construct_the_t(parameters ...);

 
Similarly, if the "buffer" inside "t" becomes complex, it gets its own allocator/deallocator. Then everything moves towards the OOP approach (as opposed to a single function with deeply nested ifs).
 
mlp
boots too small
  • Total Posts : 714
  • Reward points : 0
  • Joined: 2012/09/10 15:12:07
  • Location: previously Microchip XC8 team
  • Status: offline
Re: goto label 2018/02/13 09:18:38 (permalink)
+1 (1)
NorthGuy
jtemples
needing a bunch of "if" statements at the end of the code for clean up, which the goto code doesn't need.

...
As the code gets more complex, you don't write all the code as a single function anymore.
 

... and this way you use your returns as "structured gotos".

Mark (this opinion available for hire)
1and0
Access is Denied
  • Total Posts : 8872
  • Reward points : 0
  • Joined: 2007/05/06 12:03:20
  • Location: Harry's Gray Matter
  • Status: offline
Re: goto label 2018/02/13 09:42:45 (permalink)
+3 (3)
The Linux kernel coding style guide gives specific reasons to use gotos:

7) Centralized exiting of functions
-----------------------------------
Albeit deprecated by some people, the equivalent of the goto statement is
used frequently by compilers in form of the unconditional jump instruction.
 
The goto statement comes in handy when a function exits from multiple
locations and some common work such as cleanup has to be done. If there is no
cleanup needed then just return directly.
 
Choose label names which say what the goto does or why the goto exists. An
example of a good name could be ``out_free_buffer:`` if the goto frees ``buffer``.
Avoid using GW-BASIC names like ``err1:`` and ``err2:``, as you would have to
renumber them if you ever add or remove exit paths, and they make correctness
difficult to verify anyway.
 
The rationale for using gotos is:
 
- unconditional statements are easier to understand and follow
- nesting is reduced
- errors by not updating individual exit points when making
modifications are prevented
- saves the compiler work to optimize redundant code away ;)

jack@kksound
code tags!
  • Total Posts : 3167
  • Reward points : 0
  • Joined: 2014/05/14 10:03:19
  • Location: 0
  • Status: offline
Re: goto label 2018/02/13 11:08:50 (permalink)
+1 (1)
Amazing, 100+ posts and still going strong.sad: sad I wonder, did the OP resolve the original issues? Or get lost in the storm.
Another warning - there is no good exit strategy for this "discussion" of the evils/merits of the goto statement. Smile: Smile
bblessing
Super Member
  • Total Posts : 729
  • Reward points : 0
  • Joined: 2008/12/04 06:44:21
  • Location: Cincinnati, OH
  • Status: offline
Re: goto label 2018/02/13 11:15:07 (permalink)
+2 (2)
jack@kksound
Amazing, 100+ posts and still going strong.sad: sad I wonder, did the OP resolve the original issues? Or get lost in the storm.Another warning - there is no good exit strategy for this "discussion" of the evils/merits of the goto statement. Smile: Smile


Too bad we can't goto something else. Ba dum tss!
1and0
Access is Denied
  • Total Posts : 8872
  • Reward points : 0
  • Joined: 2007/05/06 12:03:20
  • Location: Harry's Gray Matter
  • Status: offline
Re: goto label 2018/02/13 11:31:19 (permalink)
+1 (1)
jack@kksound
Amazing, 100+ posts and still going strong.sad: I wonder, did the OP resolve the original issues? Or get lost in the storm.
 

OP's original issue in Post #1 is ***not*** the goto, but is the label. ;)
 
A label must appear before a statement, even if it's a null statement
MyLabel:;

1and0
Access is Denied
  • Total Posts : 8872
  • Reward points : 0
  • Joined: 2007/05/06 12:03:20
  • Location: Harry's Gray Matter
  • Status: offline
Re: goto label 2018/02/13 11:34:32 (permalink)
+1 (1)
bblessing
Too bad we can't goto something else. Ba dum tss!

How were you about able to quote smilies in your post???  We have covered goto and brace styles, how about indentation size? mr green: 
 
Edit: Oh, we have also covered flowchart. ;)
 
post edited by 1and0 - 2018/02/13 12:59:20
du00000001
Just Some Member
  • Total Posts : 2551
  • Reward points : 0
  • Joined: 2016/05/03 13:52:42
  • Location: Germany
  • Status: offline
Re: goto label 2018/02/13 11:52:01 (permalink)
0
Ship sank, sailor drowned.

PEBKAC / EBKAC / POBCAK / PICNIC (eventually see en.wikipedia.org)
bblessing
Super Member
  • Total Posts : 729
  • Reward points : 0
  • Joined: 2008/12/04 06:44:21
  • Location: Cincinnati, OH
  • Status: offline
Re: goto label 2018/02/13 12:10:49 (permalink)
+1 (1)
1and0
bblessing
Too bad we can't goto something else. Ba dum tss!

How were you about able to quote smilies in your post???  We have covered goto and brace styles, how about indentation size? mr green: 


This is a salient point: we're changing this post's mission statement. If the March of Dimes can go from polio to prematurity, then surely we can add indentation size. I'm at four spaces per tab, which I think is the default for MPLAB X and many others.

If you guys really wanted to kill this thread, you could walk it into the Harmony hell in a hand basket thread that got shut down.
1and0
Access is Denied
  • Total Posts : 8872
  • Reward points : 0
  • Joined: 2007/05/06 12:03:20
  • Location: Harry's Gray Matter
  • Status: offline
Re: goto label 2018/02/13 12:37:17 (permalink)
+1 (1)
Seriously, to me, OP has already got the answers to his 3 4 questions, which are
  • Question from Post #1, answers in Post #3, #4, #41, and #106.
  • Question from Post #41, answers in Post #43.
  • Question from Post #63, answers in Post #66.
Edit:
  • Question from Post #89, answers in Post #90.
... and the rest is a plus. ;)
post edited by 1and0 - 2018/02/13 12:56:11
jack@kksound
code tags!
  • Total Posts : 3167
  • Reward points : 0
  • Joined: 2014/05/14 10:03:19
  • Location: 0
  • Status: offline
Re: goto label 2018/02/13 12:52:46 (permalink)
0
1and0
bblessing
Too bad we can't goto something else. Ba dum tss!

How were you about able to quote smilies in your post???  We have covered goto and brace styles, how about indentation size? mr green: 


Yea, how did you include the smilies in the quote? 
qɥb
Monolothic Member
  • Total Posts : 3332
  • Reward points : 0
  • Joined: 2017/09/09 05:07:30
  • Location: Jupiter
  • Status: offline
Re: goto label 2018/02/13 14:02:09 (permalink)
+1 (1)
jack@kksound
...
Yea, how did you include the smilies in the quote?

Just a guess, but many things behave differently when you're using the "Mobile" them rather than "Elegant".
"Mobile" is auto selected when you're accessing the forum through a phone.
 

This forum is mis-configured so it only works correctly if you access it via https protocol.
The Microchip website links to it using http protocol. Will they ever catch on?
PicForum "it just works"
bblessing
Super Member
  • Total Posts : 729
  • Reward points : 0
  • Joined: 2008/12/04 06:44:21
  • Location: Cincinnati, OH
  • Status: offline
Re: goto label 2018/02/13 14:30:44 (permalink)
0
qɥb
jack@kksound...Yea, how did you include the smilies in the quote?

Just a guess, but many things behave differently when you're using the "Mobile" them rather than "Elegant"."Mobile" is auto selected when you're accessing the forum through a phone. 

This sounds right, as I'm posting from my phone.
1and0
Access is Denied
  • Total Posts : 8872
  • Reward points : 0
  • Joined: 2007/05/06 12:03:20
  • Location: Harry's Gray Matter
  • Status: offline
Re: goto label 2018/02/13 15:31:35 (permalink)
+2 (2)
jack@kksound
Amazing, 100+ posts and still going strong.sad: sad I wonder, did the OP resolve the original issues? Or get lost in the storm.Another warning - there is no good exit strategy for this "discussion" of the evils/merits of the goto statement. Smile: Smile
Testing quoting smilies in Mobile theme on a PC ...
Peter Sikora
Super Member
  • Total Posts : 132
  • Reward points : 0
  • Joined: 2003/11/07 12:46:46
  • Location: Brisbane Australia
  • Status: offline
Re: goto label 2018/02/20 04:50:47 (permalink)
+1 (1)
1and0
Edit: Oh, we have also covered flowchart. ;)

Thanks for the mention. I am sure an academic will one day create a standardized flow charting method for software. I think that some missed the point. Like with technical drawing it is a means to communicate ideas so that everyone can understand and share the idea in non ambiguous ways. A flowchart doesn't need to follow every intimate detail of the code. Just the structure. It can be difficult to follow comments from someone elses work but a picture speaks 1000 words.


Page: << < ..6 Showing page 6 of 6
Jump to:
© 2019 APG vNext Commercial Version 4.5