Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-4041-

goto is good

Name: Anonymous 2012-06-11 22:02

if you think otherwise you're a trendfag. goto is far more efficient than function calls for short blocks of code (lol compiler ignoring inline) and have a lot of neat applications.

Name: Anonymous 2012-06-11 22:07

Every programmer worth their salt knows this.

Name: Anonymous 2012-06-11 22:08

only if you goto back to the spot where you first goto in order to make your code read in stepwise fashion

Name: Anonymous 2012-06-11 22:17

I thoroughly enjoy spaghetti code.
I enjoy not being able to follow logic properly because of lack of structure.
I enjoy investing an inordinate amount of time figuring out logical problems in spaghetti code because the idiot coder actually believes in premature optimization.
Godspeed to you good people, and may the fruits of your work be evident.

Name: Anonymous 2012-06-11 22:27

i know i'm dumb, but this is the only way i could figure out fizz buzz without a separate case

void fizz_buzz(int upto)
{
  int i = 1;
  while(i != upto+1)
  {
    if(i % 3 == 0)
    {
      printf("%s", "Fizz");
    }
    else
      goto not_multiple_of_3;

    if(i % 5 == 0)
    {
      printf("%s", "Buzz");
    }
    goto new_line;

not_multiple_of_3:
    if(i % 5 == 0)
    {
      printf("%s", "Buzz");
      goto new_line;
    }
    printf("%d", i);

new_line:
    printf("\n");

    i++;
  }
}

Name: Anonymous 2012-06-11 22:29

step 1. Use a good compiler.
step 2. Let the compiler do it.

Name: Anonymous 2012-06-11 22:57

>>1
nope 0/10 come back after java 101

Name: Anonymous 2012-06-11 23:07

goto is great, but people is stupid so they can't understand how to use it correctly

Name: Anonymous 2012-06-11 23:16

Only basicfags use goto.  They can't program for shit.

Name: Anonymous 2012-06-12 0:00

>>5
1/10 my trolling standards are far too high

Name: Anonymous 2012-06-12 0:16

void fizz_buzz(int upto)
{
    if (upto > 1)
    {
        fizz_buzz(upto - 1);
    }

    if (upto % 3 == 0 & upto % 5 == 0) { printf("FizzBuzz"); }
    else if (upto % 3 == 0) { printf("Fizz"); }
    else if (upto % 5 == 0) { printf("Buzz"); }
    else { printf("%d", upto); }
}

Name: Anonymous 2012-06-12 0:19

even asm coders prefer function calls to jmp

Name: Anonymous 2012-06-12 1:27

gcc: __attribute__((always_inline)) or __attribute__((flatten))
MSVC: __forceinline or I have no clue if MSVC has an equivalent to flatten.

Name: Anonymous 2012-06-12 1:32

Kazushige Goto

Name: Anonymous 2012-06-12 1:43

>>6
name one

Name: Anonymous 2012-06-12 1:47

>>15
It's probably one you've never heard of. He made it when he was 12 years old!

Name: Anonymous 2012-06-12 2:01

goto can be used to implement a state machine rather than keeping a state variable and switching on it.


#include <stdlib.h>
#include <stdio.h>

int is_even(int n) {
    if (n < 0) n = -n;
even:
    if (n == 0) return 1;
    n--;
    goto odd;
odd:
    if (n == 0) return 0;
    n--;
    goto even;
}

int main(int argc, char **argv) {
    if (argc != 2 ) {
    fprintf(stdout, "Usage: %s <n>\n", argv[0]);
    exit(EXIT_FAILURE);
    } else {
    int n = atoi(argv[1]);
    puts(is_even(n) ? "true" : "false");
    }
    return 0;
}

Name: Anonymous 2012-06-12 2:11

>>15
A compiler that achieves the theoretical bound for awesomeness in a compiler. Or one that implements all of the following:

http://en.wikipedia.org/wiki/Dead_code_elimination
http://en.wikipedia.org/wiki/Loop_optimization
http://en.wikipedia.org/wiki/Loop_fission
http://en.wikipedia.org/wiki/Loop_fusion
http://en.wikipedia.org/wiki/Loop_interchange
http://en.wikipedia.org/wiki/Loop_inversion
http://en.wikipedia.org/wiki/Loop-invariant_code_motion
http://en.wikipedia.org/wiki/Automatic_parallelization
http://en.wikipedia.org/wiki/Loop_scheduling
http://en.wikipedia.org/wiki/Loop_skewing
http://en.wikipedia.org/wiki/Software_pipelining
http://en.wikipedia.org/wiki/Loop_splitting
http://en.wikipedia.org/wiki/Loop_tiling
http://en.wikipedia.org/wiki/Vectorization_(parallel_computing)
http://en.wikipedia.org/wiki/Loop_unrolling
http://en.wikipedia.org/wiki/Loop_unswitching
http://en.wikipedia.org/wiki/Loop_optimization#The_unimodular_transformation_framework
http://en.wikipedia.org/wiki/Loop_optimization#The_polyhedral_or_constraint-based_framework
http://en.wikipedia.org/wiki/Scalable_parallelism
http://en.wikipedia.org/wiki/Scalable_locality
http://en.wikipedia.org/wiki/Loop_nest_optimization
http://en.wikipedia.org/wiki/Duff's_device
http://en.wikipedia.org/wiki/Data-flow_analysis
http://en.wikipedia.org/wiki/Control_flow_graph
http://en.wikipedia.org/wiki/Basic_block
http://en.wikipedia.org/wiki/Static_code_analysis
http://en.wikipedia.org/wiki/Model_checking
http://en.wikipedia.org/wiki/Abstract_interpretation
http://en.wikipedia.org/wiki/Constant_folding
http://en.wikipedia.org/wiki/Sparse_conditional_constant_propagation
http://en.wikipedia.org/wiki/Control_flow_graph#Domination_relationship
http://en.wikipedia.org/wiki/Control_flow_analysis
http://en.wikipedia.org/wiki/Reaching_definitions
http://en.wikipedia.org/wiki/Liveness_analysis
http://en.wikipedia.org/wiki/Definite_assignment_analysis
http://en.wikipedia.org/wiki/Available_expression
http://en.wikipedia.org/wiki/Interprocedural_optimization

Name: Anonymous 2012-06-12 2:19

>>17

IcUredURsicp2DEH


int even(int n) { return n == 0 || odd(n - 1); }
int odd(int n) { return n == 1 || even(n - 1); }

Name: sage 2012-06-12 2:22

forgot:


int is_even(int n) { if(n < 0) return even(-n); else return even(n); }
int is_odd(int n) { if(n < 0) return odd(-n); else return odd(n); }

Name: Anonymous 2012-06-12 6:16

>>17
int is_even(int n){
    return !(n & 1)

Name: Anonymous 2012-06-12 7:26

goto is acceptable when you use it for error handling, like this:
[code]int foo()
{
        /* Allocate memory */
        if (/* error */)
                goto error;
        return 0;
error:
        fprintf(stderr, "Error occurred!\n");
        /* Release memory */
        return -1;
}

Name: Anonymous 2012-06-12 8:12

>>22
What's wrong with the if statement?
int foo()
{
        /* Allocate memory */
        if (/* correct */)
            return 0;
        else //assume an error occurred
        {
            fprintf(stderr, "Error occurred!\n");
            return -1;
        }
}

Name: Anonymous 2012-06-12 8:31

>>23
there's a lot of yiffing, you could instead
#define TRY(X, Y) if(X) goto Y;
since encapsulating the section of error handling is unpractical.

#define TRY(X, Y) if(X) goto Y;
int func(){
    TRY(func2(23), EAGAIN);
    return 0;
    EAGAIN:
          fprintf(stderr, "Error occurred!\n");
            return -1;
}

Name: Anonymous 2012-06-12 8:43

goto's create a 3 dimensional execution path, which is almost impossible to debug.  Making goto's shit.  Also lisp is shit, C for life, C is shit.

Name: Anonymous 2012-06-12 9:03

>>23
If you have to allocate more than one pointer:
int foo(size_t size1, size_t size2) {
    char* string1;
    char* string2;
    if (!(string1 = malloc(size1))
        goto error;
    if (!(string1 = malloc(size2)) {
        /* free(string1); */
        goto error;
    }
    return 0;
error:
    fprintf(stderr, "%s: malloc: %s\n", __func__, strerror(errno));
    free(string1);
    free(string2);
    return -1;
}

Otherwise you'd have to free all the pointers you allocated before the error in every subsequent if, which could get ridiculous.

>>24
If you want to get really nasty, take a look at this library I wrote (abridged):
#define throw(type,  ...)\
    do {\
        __throw(type, __FILE__, __func__, __LINE__, __VA_ARGS__);\
        longjmp(__ex_jmp_buf, type);\
    } while (0)
#define try        if (!(__ex_type = setjmp(__ex_jmp_buf)))
#define catch(type) \
    else if ((__ex_type != EXCEPTION_NONE) && (type == EXCEPTION_ANY || __ex_type == type)) {\
        exception* e = __catch();\
        __unthrow();
#define otherwise    } else


Example:
#include <exception.h>

int idiv(int numerator, int denominator)
{
    if (denominator == 0)
        throw(EXCEPTION_DIVISION_BY_ZERO, "Integer division by zero");
    return numerator / denominator;
}

int main()
{
    try {
        idiv(1, 0);
    } catch (EXCEPTION_ANY) {
        exception_print(e);
    } otherwise {
        printf("No exceptions occurred.\n");
    }
    return 0;
}

Name: Anonymous 2012-06-12 10:12

>>26
In your first code, if the first malloc doesn't work it will try to free the second pointer which has a unknown value.

Name: Anonymous 2012-06-12 10:27

>>27
You're right - I should have initialised the two pointers to NULL first. I also typed 'string1' instead of 'string2' in the second 'if'. I'm breaking all the rules today.

Name: Anonymous 2012-06-12 10:45

Also, is free(NULL); correct ?

Name: Anonymous 2012-06-12 10:48

>>29
free(3)
If ptr is NULL, no operation is performed.

Name: Anonymous 2012-06-12 11:04

>>30
Is it glibc's answer or POSIX's ?

Name: Anonymous 2012-06-12 11:16

>>31
It applies to all standards-compliant implementations, if that's what you're asking.

Name: Anonymous 2012-06-12 11:18

Name: Anonymous 2012-06-12 11:22

>>17
fucking this

Name: Anonymous 2012-06-12 11:25

>>11
if (upto % 3 == 0 & upto % 5 == 0) { printf("FizzBuzz"); }

There still is this separate comparison. Try doing it without it (also without a helper variable). You have to use goto.

Name: Anonymous 2012-06-12 11:25

>>34
>>17
You could also create a jump table (I think that's what compilers tend to do if you have a large switch statement anyway).

Name: Anonymous 2012-06-12 11:27

>>22
Sure seems a lot faster than sepples smart pointers and exceptions.

Name: Anonymous 2012-06-12 11:30

>>37
It probably is faster, but sepples is about compile-time safety or something. C++ has improved with the new standard, though, and I'm starting to use it a little more. I still mostly use C, C# and FIOC though.

Name: Anonymous 2012-06-12 13:15

my philosophy:

as long as you can figure out what you wrote, its plenty readable

Name: Anonymous 2012-06-12 13:51

>>35
Dafuq u talking about?  This works.

Name: Anonymous 2012-06-12 16:00

>>40
yes it does, but can you do it without checking for "FizzBuzz" explicitly? also without a helper variable?

Name: Anonymous 2012-06-12 16:18

void fizzbuzz(int end){
    int i;
    for(i = 3; i < end; i += 3)
        printf("Fizz\n");
    for(i = 5; i < end; i += 5)
        if(i % 3)
            printf("Buzz\n");
        else
            printf("FizzBuzz\n");
}


if order is not important

Name: Anonymous 2012-06-12 16:24

>>42
that is actually wrong, forgive me /prague/

void fizzbuzz(int end){
    end /= 15:
    while(end--)
        printf("Fizz\nBuzz\nFizz\nFizz\nBuzz\nFizz\nFizzbuzz\n");
}

Name: Anonymous 2012-06-12 16:38

>>43
That's also wrong because you're supposed to print the number for ones that aren't fizz/buzz/fizzbuzz, e.g.
1
2
fizz
4
buzz
etc.

Name: Anonymous 2012-06-12 16:51

>>44
oh right
static char * arr[] = {"%d", "%d", "Fizz", "%d", "Buzz", "Fizz", "%d", "%d", "Fizz", "Buzz", "%d", "Fizz", "%d", "%d", "FizzBuzz"};
int fizbuzz(int end){
int i;
for(i = 0; i < end; ++i)
printf(arr[i%15], i);
}

Name: Anonymous 2012-06-12 21:51

void fizzbuzz(size_t upto)
{
    char *text[] = {"FizzBuzz\n","%d\n","%d\n","Fizz\n","%d\n","Buzz\n","Fizz\n","%d\n","%d\n","Fizz\n","Buzz\n","%d\n","Fizz\n","%d\n","%d\n"};

    for(size_t i = 0; i < upto; ++i)    printf(text[i%15], i);
}

hai gaiz cna i play wit u?

Name: Anonymous 2012-06-12 22:39

thinks using gotos for simple control flow is faster than using built in syntax for equivalent control flow

there are times to use goto, but this is not one of them. I love the art of goto programming as much as anyone, but don't act like any of this is actually useful for anything other than fun.

Name: Anonymous 2012-06-12 22:54

>>47
equivalent

Name: Anonymous 2012-06-12 23:01

I have nothing against goto, but "efficiency" is the worst possible reason to use it.

Name: Anonymous 2012-06-12 23:19

>>48
there is a transformation. while is all you need for any control flow, although extra variables may need to be introduced. tail calls and if can be trivially compiled to a convoluted bundle of gotos, with no additional resources as well. Any arbitrary block of goto code can be translated to a set of functions that transfer control to one another with tail calls.

Name: Anonymous 2012-06-13 0:13

>>17,20

Lambda Calculus.

int is_even(int n)
{
    return !(n & 1);
}

Name: Anonymous 2012-06-14 3:45

congrats op, your  thread is at the very bottom of the board.

how does it feel, retard?

Don't change these.
Name: Email:
Entire Thread Thread List