Do you use goto? I program in C for 5 years now and didn't use it once

Do you use goto? I program in C for 5 years now and didn't use it once.

Attached: working-goto.jpg (369x200, 8K)

Other urls found in this thread:

emulators.com/docs/nx25_nostradamus.htm
gcc.gnu.org/onlinedocs/gcc/Nested-Functions.html
twitter.com/NSFWRedditVideo

my professor said not to use it because it makes "spaghetti code"

How do you do error handling in c without it

only going down, only for error handling. good enough for linus - good enough for me.

i don't

heh nothin personel kid

>Do you use goto?
Of course. It's the best thing to represent state machine.

she asked me for a way out I gave it to her, I had to take extreme meassures to make it worth the problems

only for some small pics that don't support BRA

yes I use Gentoo

>t. too dumb for LFS
kys

Without goto:

A = allocate(A);
if (!A)
{
exit_with_error();
}

B = allocate(B);

if (!B)
{
deallocate(A);
exit_with_error();
}

C = allocate(C);

if (!C)
{
deallocate(B);
deallocate(A);
exit_with_error();
}
/* ... */

With goto:

A = allocate(A);

if (!A)
{
goto A_FAILED;
}

B = allocate(B);

if (!B)
{
goto B_FAILED;
}

C = allocate(C);

if (!C)
{
goto C_FAILED;
}

/* ... */

C_FAILED: deallocate(B);
B_FAILED: deallocate(A);
A_FAILED: exit_with_error();

Goto is helpful when dealing with errors. It is quite common in the Linux kernel.

Rarely

>goto error;

how does goto work with unrolling the function stack?

Building a LFS system is easy and boring. Updating the system afterwards is where it gets very annoying, unless you build some tools for yourself to automate most of it.. in which point you have a simple package manager.. why not use Gentoo/Crux/whatever instead?

I use it quite often. I like to flatten things so as to not have big if-pyramids. I haven't seen a case of "spaghetti goto" that wasn't intentionally made to look bad.
I see lots of code like this and it really bothers me. It is so ugly:
void sample()
{
if (a) {
if (b) {
if (c) {
/* inner statements */
}
/* lorem */
}
/* ipsum */
}
/* dolor */
}


Instead I would prefer to write it like this:
void sample()
{
if (!a)
goto dolor;

if (!b)
goto ipsum;

if (c) {
/* inner statements */
}

/* lorem */
ipsum;
/* ipsum */
dolor:
/* dolor */
}

where the hell did you learn to program
how can you find the second example clearer than the first
have you been using gotos your whole life

A wild opposing opinion appears!
>have you been using gotos your whole life
No, the first languages I used did not have it.

goto fail;
goto fail;

Attached: anon.jpg (750x1000, 35K)

No, it's not MISRA compliant.

It's not an opinion, there's just no way that second example is clearer unless you've been gotoing your whole life, and I started with qbasic

neat

>It's the best thing to represent state machine.
switch/case you fucking suckless brainlet

I don't want to do an useless if when I already know on which branch I am. I'm against the idea of useless assignation, useless check etc. If it's useless, don't put it, it's just disturbing noise.

I substitute break for goto, I think the code looks clearer when you have too many things nested.

Is that how they do it? I just call a function.

Attached: 1499479662483.png (528x404, 8K)

As seen here, it can avoid copy paste errors

>C_FAILED: deallocate(B);
> B_FAILED: deallocate(A);
> A_FAILED: exit_with_error();
Make deallocate() capable of handling NULL as argument instead, then just add a single FAIL label.

i think i did one time for a really small single-file game i made for a competition ages ago. i used sdl for the project itself, but the goto was used like this:
// game loop
for (;;) {
SDL_Event e;
while (SDL_PollEvent(&e)) {
if (e.type == SDL_QUIT) goto quit;
}
// game-related shit here, timing stuff omitted...
}

quit:
// free resources, quit sdl, etc.


could obviously write this differently without goto but in any case

>write efficient/understandable code
>use goto to bypass an entire block of image processing if an error ever occurs and clean up all of the resources the library would use
>reduces code-size by a ton
>zero down-sides

>scrubs on Github won't merge it because "goto considered harmful"

I typically use the return value of functions to represent status messages (e.g. errors) then pass pretty much everything by reference. "ok" is always zero. Makes handling of errors very consistent.

t. brainlet who thinks he knows C

That was a problem with not using braces for single-line conditions, not with goto

Goto is useful in C since it lacks try-catch style exception handling, if you're using gcc or clang then you also have the option of using computed goto which lets you write certain things like bytecode interpreters more efficiently than you can without them: emulators.com/docs/nx25_nostradamus.htm

This technique is used in CPython to make opcode dispatch faster by taking advantage of the branch predictors on modern CPU's and the fact that certain bytecode instructions tend to come in pairs

I always do, I like how my colleges burst in flames due to this.

It doesn't. Don't do that shit. What you need is setjmp and longjmp.

It looked more readable to me... and I'm not a programmer.

C needs a # after the break keyword where # is the number of nested brackets it breaks out of.

Every open-source network stack I can think of is riddled with gotos.

>my professor said not to use it because it makes "spaghetti code"
The world would be a much better place if professors teaching language X had to list everything they've written in X.

I have a suspicion that at least 95% of professors who teach C haven't written anything beyond exercise-tier toy programs. It should be a job requirement that they've contributed large bodies of code to open-source projects for at least 10 years and that the code has been scrutinized for security and robustness.

This has to be an age thing, one of our older developers does this shit all the time.

In one thing he actually broke out of a loop with labels and went back into it. fucking weird.

>It should be a job requirement that they've contributed large bodies of code to open-source projects for at least 10 years and that the code has been scrutinized for security and robustness.

There would be no professors

>It should be a job requirement that they've contributed large bodies of code to open-source projects for at least 10 years and that the code has been scrutinized for security and robustness.
People who get to this point take a huge pay cut by switching to academia, this is why it's so rare

Just let the program crash. It's most likely the user's fault anyway.

>it's not an opinion, there's JUST-
c'mon now helloworld-sama, you should consider that the second example allows the author to logically separate (and swap out) the means to an end and the potential end of said means; it's always better to avoid nesting

Just make deallocate() work with null pointers and deallocate all three every time any of them fail. If B and C haven't yet been allocated then with null pointer acceptance nothing bad will happen.

>branch predictors on modern CPU's
Those are dead now thanks to vulnerabilities.
Enjoy your insecure code.

you're a fucking terrorist if you use gotos in the 21st century.

Attached: 1537496490313.jpg (450x400, 26K)

If you're in the opcode dispatch loop of a virtual machine then the client code isn't in control at that point, are you sure it's really a concern in that particular case?

Yes. What if I want to exit a 2d array without having to use a bool and check once per loop?

Is error handling where there is no try...catch...finally the only valid use case for goto?

You should generally be using switches in high level languages instead of goto statements.
int exampleFunction(void) {
void scopedFree(unsigned char);

if (!(char *a_buf = malloc(128*sizeof(char)))) {
scopedFree(1);
return -1;
}
else if (!(char *b_buf = malloc(128*sizeof(char)))) {
scopedFree(2);
return -2;
}
else if (!(char *c_buf = malloc(128*sizeof(char)))) {
scopedFree(3);
return -3;
}
//
scopedFree(4);
return 0;

void scopedFree(unsigned char pos) {
switch (pos) {
case 4:
free(c_buf);
case 3:
free(b_buf);
case 2:
free(a_buf);
case 1:
default:
return;
}
}
}

No:

Trips of zero IQ level

The second example avoids deep nesting, this can be helpful for legibility even if inverting conditions takes some getting used to

You know what else avoids deep nesting? Understanding how logic works. Instead of either of you can just use the following:

void sample() {
if (c && b && a) {
// inner statements
}
if (b && a) {
// lorem
}
if (a) {
// ipsum
}
// dolor
}

we're talking about computers, not logic.

That is nonstandard functionality, defining scopedFree in the body of the function. Additionally, every nerve in my body aches at the sight of all of the unnecessary magic numbers you're just tossing in.

>That is nonstandard functionality, defining scopedFree in the body of the function.
It's literally standard scoping.

>unnecessary magic numbers
Do elaborate. I'm quite interested to see if you think counter variables in for loops are magic numbers as well.

Nested functions in C are a gcc compiler extension, it's not standard: gcc.gnu.org/onlinedocs/gcc/Nested-Functions.html

I stand corrected.
I must have confused the silly shit I did because of malloc/free with the stuff I felt should have worked after writing a shitty compiler. The correct standard way to do what I wanted should probably then involve passing structs and/or making a custom map or wrapper for malloc that holds the pointers of successful calls so you can tell it to free everything with a single call.
I'm now starting to think that I may have subconsciously blocked the memories of doing that years ago for a reason.

at this point why not use functions? Aren't you just using the labels as functions?

OOF!

I don't like pyramids either but I use returns and breaks instead of gotos.

>my professor said not to use it because it makes "spaghetti code"
Your professor is full of shit. goto mechanism is best suited for error handling, usecase:

You lock the mutex and then you have 10 branches which can fail, so do you unlock the mutex in each of 10 locations or do you goto to mutex unlock with value return?

There are two use cases for goto in C, and both are very specific.

One is to exit out of a nested loop early, similar to break, but not limited to a single loop.

The other concerns error handling. If one allocates resources in a function multiple times, and one suddenly comes across an error, it would suck to have to duplicate the same cleanup code every time you want to return early. Therefore, it would make sense to simply set a return value, and then jump to the end of the function where everything gets cleaned up.

This secondary use case becomes unnecessary in C++, where RAII can allow cleanup to be handled automatically.

These guys are onto something

It doesn't, it's just a jmp instruction

>junior web developer

You're fucking piece of shit.
>goto is jmp instruction
>function obligates to stack frame, call conventions, a lot of redundancy if you wish to have sepcialized error handling for each case
This is how windows is made, bunch of monkeys complicating things

why are you using c in the first place?

>i don't know about function inlining: the post

with c++/raii:

try
{
A a;
B b;
C c;
/* ... */
}
catch(...)
{
exit_with_error();
}

there are people who have coded for 10 years in industry and open source who are terrible programmers.

kek

This. Goto cleanup is my most prominent use of goto.

Infinite loops and jumping within while/for loops.

Are there any other languages that implement goto labels similar to Powershell? They allow circling between nested loops and are only limited to them. Here's an official example:
:red while ()
{
:yellow while ()
{
while ()
{
if ($a) {break}
if ($b) {break red}
if ($c) {break yellow}
}
# After innermost loop
}
# After "yellow" loop
}
# After "red" loop

Labels work with continue as well there.

>Do you use goto
Yes, its required in c# in order to fallthrough in a switch statement.

>76 replies
>one mention of setjmp.h

does anyone in this thread actually write C for a living instead of jerking off to the idea?

The C standard defines sizeof(char) as 1. If typing it out is a conscious style choice for whatever reason, that's fine, I guess, but it provides no technical advantages, at least not for the code given (I can think of some contrived code in which the introduction of a size_t via an appropriately placed sizeof stops integer overflow).

Doesn't matter.

Complete the work first, then optimize. Wasting time optimizing before the program is created is useless. There will always be a better way to do things, so in chasing such thing before you're done with programming, you will never finish your project.

If you're using goto as some kind of replacement for normal branching, then you're literally going against the language and you're causing yourself problems in the long run. If you have a specific problem that could be easily solved with a goto, then use it. This isn't rocket science.

Ever heard of an 'if' statement? return status values?

Welcome to Jow Forums. Most people here are

a) underage highschool students
b) NEETs for life
c) web devs

please, make comments only if you ever actually worked on some real-life C programs

>sizeof(char)
user, i...

Sometimes to exit nested while loops

yeah it's quite useful there

I've been writing C/C++ professionally, for over 10 years, last 4 constantly, and you never need goto
If you think you need to use it, then redesign your program flow.

I think java has that too