goto poll;
- piranha
- Member
- Posts: 1391
- Joined: Thu Dec 21, 2006 7:42 pm
- Location: Unknown. Momentum is pretty certain, however.
- Contact:
goto poll;
What do you think of the goto keyword?
Good/Bad etc...
I don't use it, not because of a general rule, just because other ways look cleaner and are easier to read and deal with.
-JL
Good/Bad etc...
I don't use it, not because of a general rule, just because other ways look cleaner and are easier to read and deal with.
-JL
SeaOS: Adding VT-x, networking, and ARM support
dbittman on IRC, @danielbittman on twitter
https://dbittman.github.io
dbittman on IRC, @danielbittman on twitter
https://dbittman.github.io
- Colonel Kernel
- Member
- Posts: 1437
- Joined: Tue Oct 17, 2006 6:06 pm
- Location: Vancouver, BC, Canada
- Contact:
If there were an option in the poll that says "I use it as a last resort", that's what I would have picked. It can be handy for centralizing last-ditch cleanup code in C if you have no other choice (RAII in C++ is a much better way to do it). I wouldn't use it for any other purpose though.
Top three reasons why my OS project died:
- Too much overtime at work
- Got married
- My brain got stuck in an infinite loop while trying to design the memory manager
- AndrewAPrice
- Member
- Posts: 2299
- Joined: Mon Jun 05, 2006 11:00 pm
- Location: USA (and Australia)
You have to be careful too. You can jump from a member in one class to a member in another, which really messes up the stack.
EDIT: My lecturer also said it's an instant fail if he finds a goto in one of our assignments.
EDIT: My lecturer also said it's an instant fail if he finds a goto in one of our assignments.
My OS is Perception.
- piranha
- Member
- Posts: 1391
- Joined: Thu Dec 21, 2006 7:42 pm
- Location: Unknown. Momentum is pretty certain, however.
- Contact:
Well, I hope he knows about the jump command for the CPU.......it's freakin everywhere.EDIT: My lecturer also said it's an instant fail if he finds a goto in one of our assignments.
I bet he does, but yeah....
And it's true, goto isn't great.
SeaOS: Adding VT-x, networking, and ARM support
dbittman on IRC, @danielbittman on twitter
https://dbittman.github.io
dbittman on IRC, @danielbittman on twitter
https://dbittman.github.io
I *do* use it in C occassionally, but only as a last resort for in-function exception handling. (For example, during construction of nested memory structures on the heap - if the second or third allocation fails, you have to clean up what you already allocated.) I could write my way around using goto, but sometimes the workaround would be even uglier.
I didn't vote any of the above because none of the options quite fit.
I didn't vote any of the above because none of the options quite fit.
Every good solution is obvious once you've found it.
The only time I use the goto: is when I am handling interrupts and do last minute error checking.
Website: https://joscor.com
- FutureDomain
- Posts: 7
- Joined: Fri Aug 10, 2007 8:43 am
- Location: Evansville, IN
- Contact:
I never use goto in C#. It's there, but I find it's better to use structured programming and exception handling. There are a couple circumstances where it might be the best option, but I've never hit those yet.
FutureDomain: Everybody's favorite software maverick.
Xenon: Rethinking the operating system.
Xenon: Rethinking the operating system.
- AndrewAPrice
- Member
- Posts: 2299
- Joined: Mon Jun 05, 2006 11:00 pm
- Location: USA (and Australia)
It is still considered by many as bad programming practice. I take it as you haven't used C or C++ before? Well, it is a simple jump instruction, which may work fine in some situations (e.g. for a to jump over a block of code, or back to the beginning of a loop), but when you start jumping between functions, classes, and between scope within the same function, you can begin to screw up the stack and memory location of local variables. This is not to say you couldn't work out a way to do this in a safe manner.XCHG wrote:Isn't "goto" in C the equivalent of a simple JMP instruction? I don't see any problems in using that.
Of course you have to use the JMP instruction to divert program control to another section in memory, and goto isn't the only way to do this. C loops, conditions, and function calls removes the necessity of using goto. I don't even have a single "goto" in my OS which is 99%+ C++ (I do use jmp in assembly, but only occasionally, like to jump over my multiboot header and to jump to my main c++ function). This is only my opinion, and does not mean that you can't/shouldn't use goto just because I say so.XCHG wrote:You have to use it. If you don't, people will die
I could provide useful in some situations, like if you loaded a program and you wanted to jump to an address in memory. In my OS, I load the EIP into the thread's structure, which gets placed into memory on a context switch, avoiding the need for goto. If I had to jump to an address, I would create a function pointer to the address and then call the function.
My OS is Perception.
- Brynet-Inc
- Member
- Posts: 2426
- Joined: Tue Oct 17, 2006 9:29 pm
- Libera.chat IRC: brynet
- Location: Canada
- Contact:
Yes, and for people that let the compiler do most/all of the work... in there lies the problem of diverting program control in such a manner.XCHG wrote:Isn't "goto" in C the equivalent of a simple JMP instruction? I don't see any problems in using that. You have to use it. If you don't, people will die
I try hard not to use it. But to my everlasting shame I have used it in hobby code. Every time I have used it it has been because I did not truly understand the problem and the resulting solution...
http://www.cs.utexas.edu/users/EWD/ewd02xx/EWD215.PDF
http://www.cs.utexas.edu/users/EWD/ewd02xx/EWD215.PDF
Thanks for all the fish.