Examples of good gotos in C or C

0 votes

In this thread, we'll look at some interesting goto examples in C or C++.

infinite_loop:

    // code goes here

goto infinite_loop;

Why it's better than the alternatives:

1. It is precise. The goto language construct generates an unconditional branch. 

Alternatives rely on structures that enable conditional branching and a degenerate always-true condition.

2. The label expresses the aim without additional commentary.

3. The reader is not required to inspect the intervening code for early breaks (but an unscrupulous hacker might still fake continuing with an early goto).

Aug 2 in C++ by Nicholas
• 5,020 points
15 views

1 answer to this question.

0 votes

Here's one method I've heard of folks use. But I've never seen it in the wild. 

And that only pertains to C since RAII allows you to accomplish this more idiomatically in C++.

void foo()
{
    if (!doA())
        goto exit;
    if (!doB())
        goto cleanupA;
    if (!doC())
        goto cleanupB;

    /* everything has succeeded */
    return;

cleanupB:
    undoB();
cleanupA:
    undoA();
exit:
    return;
}
answered Aug 2 by Damon
• 4,960 points

Related Questions In C++

0 votes
0 answers

Use of min and max functions in C++

Are std::min and std::max better than fmin ...READ MORE

Jun 2 in C++ by Nicholas
• 5,020 points
32 views
0 votes
1 answer

Use of "this" keyword in C++ [duplicate]

Yes, it is optional and generally omitted.  However, it may be essential for accessing variables after they have been overridden in the scope: Person::Person() { int age; ...READ MORE

answered Jun 20 in C++ by Damon
• 4,960 points
21 views
0 votes
1 answer

how could I use the power function in c/c++ without pow(), functions, or recursion

It is part of a series.  Replace pow() with the previous iteration's value. There is no need for code to call pow ().  Pow(x, 5 * I - 1) and pow(-1, I - 1) may be formed since both have an int exponent dependent on the iterator I from the previous loop iteration. Example: Let f(x, i) = pow(x, 5 * i ...READ MORE

answered Jun 21 in C++ by Damon
• 4,960 points
49 views
0 votes
1 answer

What range of values can integer types store in C++

You may rely on the following minimal ...READ MORE

answered Jun 21 in C++ by Damon
• 4,960 points
20 views
0 votes
1 answer

The equivalent of a GOTO in python ​​

Gotos are universally reviled in computer science ...READ MORE

answered Sep 3, 2018 in Python by Priyaj
• 58,080 points
11,623 views
0 votes
1 answer

setuptools: build shared libary from C++ code, then build Cython wrapper linked to shared libary

There is a seemingly undocumented feature of setup that ...READ MORE

answered Sep 11, 2018 in Python by Priyaj
• 58,080 points
206 views
0 votes
1 answer

setuptools: build shared libary from C++ code, then build Cython wrapper linked to shared libary

There is a seemingly undocumented feature of setup that ...READ MORE

answered Sep 21, 2018 in Python by Priyaj
• 58,080 points
1,413 views
0 votes
1 answer

How to pass large records to map/reduce tasks?

Hadoop is not designed for records about ...READ MORE

answered Sep 25, 2018 in Big Data Hadoop by Frankie
• 9,830 points
807 views
0 votes
1 answer

Cases of static and dynamic binding in C++

When an object's static type is used to associate it with a member function, this is known as static binding (understand the type of its class). When a pointer or reference is associated with a member function based on the dynamic type of the object, this is known as dynamic binding (understand the instance of the variable at runtime). Before continuing, keep in mind that dynamic binding only works with pointers, references, and virtual functions for the base class. Because everything needed to call the function is known at compile time, the first call is a static binding (also known as early binding). Derived1 d1(1, 10); d1.display_data(); You already know that the d1 instance is a Derived1 automatic variable, and that it will call the Derived1::display data method (). The first condition is incorrect: d1 is neither a pointer nor a reference. The second condition isn't acceptable:  There is no virtual Derived1::display data. The second call is for ...READ MORE

answered Jun 7 in C++ by Damon
• 4,960 points
23 views
0 votes
1 answer

Use of min and max functions in C++

The functions fmin and fmax are designed ...READ MORE

answered Jun 21 in C++ by Damon
• 4,960 points
46 views
webinar REGISTER FOR FREE WEBINAR X
Send OTP
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP