View previous topic :: View next topic |
Author |
Message |
techknight
Joined: 21 Apr 2008 Posts: 231
|
Posted: Sun May 25, 2014 5:18 pm Post subject: GoTo command question |
|
|
I know its bad practice to use GoTo, (never understood why, but anyway).
Is it ok to "GoTo" out of an If/select case statement without hurting anything, such as the stack? just curious.
There are several points in my program where I goto out of an If statement to prevent code execution further down the line when something doesn check out upstream.
Of course there are better ways to do this, but using GoTo isnt hurting anything as I can tell.
But I do get a random crash/reboot every GREAT once in awhile so I figured i would ask.
(BASCOM-AVR version : 2.0.7.7 ) |
|
Back to top |
|
|
albertsm
Joined: 09 Apr 2004 Posts: 5913 Location: Holland
|
Posted: Sun May 25, 2014 10:50 pm Post subject: |
|
|
well the micro could not work without its own goto (jmp). the reason why it is considered bad practice is that you loose overview of your code very quick. so i would use it only when there is no other good way.
you can safely goto anywhere in your code with some exceptions:
- do NOT goto into a function/sub that passes parameters.
- do not goto out of a functions/sub that passes parameters.
- do not goto out of an ISR since otherwise no new interrupt will occur _________________ Mark |
|
Back to top |
|
|
techknight
Joined: 21 Apr 2008 Posts: 231
|
Posted: Sun May 25, 2014 11:06 pm Post subject: |
|
|
Yea, I dont do that. But it is safe to jump out of IF statements then. |
|
Back to top |
|
|
albertsm
Joined: 09 Apr 2004 Posts: 5913 Location: Holland
|
Posted: Sun May 25, 2014 11:22 pm Post subject: |
|
|
sure when you take into account the previous points.
i have seen code with lot of gosub and goto and some conditions use GOTO while at that point in the code a GOSUB was used so a RETURN would have been right. goto will work but at some stage your simply crash. _________________ Mark |
|
Back to top |
|
|
techknight
Joined: 21 Apr 2008 Posts: 231
|
Posted: Sun May 25, 2014 11:29 pm Post subject: |
|
|
Well in pseudocode my mainloop is structured like this:
Code: |
Do
<Check the uarts code here.>
If A = this then
do some stuff
if B <> that then
Reset some flags
goto endcheck
end if
if blah blah blah
blah blah blah
else
blahblah blah
end if
else if A = that
blah blah blah
end if
<code that runs after the If block>
Endcheck:
reset flags
Loop
|
This is rough, and far what it actually is, but its showing how I am using the GoTos. I guess I could probably stick the mainloop inside another nested do loop. Then Exit Do instead of Goto. |
|
Back to top |
|
|
albertsm
Joined: 09 Apr 2004 Posts: 5913 Location: Holland
|
Posted: Sun May 25, 2014 11:37 pm Post subject: |
|
|
no problem if you use it like that. _________________ Mark |
|
Back to top |
|
|
techknight
Joined: 21 Apr 2008 Posts: 231
|
Posted: Sun May 25, 2014 11:39 pm Post subject: |
|
|
Yea, I didnt think I was having an issue with the GoTo statements. I figured I would ask because I get a random crash every now and then. So the issue might be elsewhere. And When I say random, its totally random.
It could go for hours, or even days with no issues. but then it takes just one time the CPU will either randomly reset, or itll bounce into bootloader by accident and corrupt the first block of flash memory. Ive had 1 instance of this. |
|
Back to top |
|
|
Duval JP
Joined: 22 Jun 2004 Posts: 1161 Location: France
|
Posted: Sat May 31, 2014 11:01 am Post subject: |
|
|
Hi,
Are you sure it is a pb of code ?
it could be a pb of alim. noises, bad filtration...
1000µf plus some 100nf (2 or 3) in // are a good protection.
JP
|
|
Back to top |
|
|
|