Por que meu programa fica travado durante a execução do programa mandelbrot brainf ***?

9

Queria melhorar minhas habilidades em C, por isso procuro algumas idéias do programa.

Alguém propõe criar um simples intérprete Brainf *** e depois um compilador. Então aqui estou.

Criei o intérprete e ele funciona conforme o esperado, exceto no programa Mandelbrot:

A mandelbrot set fractal viewer in brainfuck written by Erik Bosman
+++++++++++++[->++>>>+++++>++>+<<<<<<]>>>>>++++++>--->>>>>>>>>>+++++++++++++++[[
>>>>>>>>>]+[<<<<<<<<<]>>>>>>>>>-]+[>>>>>>>>[-]>]<<<<<<<<<[<<<<<<<<<]>>>>>>>>[-]+
<<<<<<<+++++[-[->>>>>>>>>+<<<<<<<<<]>>>>>>>>>]>>>>>>>+>>>>>>>>>>>>>>>>>>>>>>>>>>
>+<<<<<<<<<<<<<<<<<[<<<<<<<<<]>>>[-]+[>>>>>>[>>>>>>>[-]>>]<<<<<<<<<[<<<<<<<<<]>>
>>>>>[-]+<<<<<<++++[-[->>>>>>>>>+<<<<<<<<<]>>>>>>>>>]>>>>>>+<<<<<<+++++++[-[->>>
>>>>>>+<<<<<<<<<]>>>>>>>>>]>>>>>>+<<<<<<<<<<<<<<<<[<<<<<<<<<]>>>[[-]>>>>>>[>>>>>
>>[-<<<<<<+>>>>>>]<<<<<<[->>>>>>+<<+<<<+<]>>>>>>>>]<<<<<<<<<[<<<<<<<<<]>>>>>>>>>
[>>>>>>>>[-<<<<<<<+>>>>>>>]<<<<<<<[->>>>>>>+<<+<<<+<<]>>>>>>>>]<<<<<<<<<[<<<<<<<
<<]>>>>>>>[-<<<<<<<+>>>>>>>]<<<<<<<[->>>>>>>+<<+<<<<<]>>>>>>>>>+++++++++++++++[[
>>>>>>>>>]+>[-]>[-]>[-]>[-]>[-]>[-]>[-]>[-]>[-]<<<<<<<<<[<<<<<<<<<]>>>>>>>>>-]+[
>+>>>>>>>>]<<<<<<<<<[<<<<<<<<<]>>>>>>>>>[>->>>>[-<<<<+>>>>]<<<<[->>>>+<<<<<[->>[
-<<+>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>]<<<<<<<<<[<<<<<<<<<]>>>>[-]<<<++++
+[-[->>>>>>>>>+<<<<<<<<<]>>>>>>>>>]>>>>>->>>>>>>>>>>>>>>>>>>>>>>>>>>-<<<<<<[<<<<
<<<<<]]>>>]

Não entendo o porquê, mas meu programa fica preso em algum tipo de loop infinito. Eu tento depurá-lo usando o gdb, mas é difícil encontrar o ponto de interrupção correto para ver por que ele travou.

O programa é o seguinte:

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

// Brainfuck instructions
//  > Math stuff
#define INCR '+' // +1 on current MS
#define DECR '-' // -1 on current MS
//  > Memory slot stuff
#define FORW '>' // Go to next MS
#define BACK '<' // Go to previous MS
//  > Logic stuff
#define SJMP '[' // Loop till current MS value is equal to zero
#define EJMP ']' // Jump to the beginning of the loop
//  > I/O stuff
#define PRTC '.' // Print character with MS value as ASCII code
#define GETC ',' // Get an user input ASCII character code

typedef unsigned char byte;

// The brainfuck program struct
typedef struct {
      byte *values;
      int size;
      int index;

      byte *loops_starts;
      int loop_size;
      int loop_index;

} BrainfuckProgram;

void initialize(BrainfuckProgram *bfp) {
      bfp->values = malloc(bfp->size * sizeof(*bfp->values));
      bfp->loops_starts = malloc(bfp->size * sizeof(*bfp->loops_starts));
}

int allocate_values(BrainfuckProgram *bfp) {
      byte *newMem = realloc(bfp->values, bfp->size * sizeof(byte));

      if (!newMem) {
            return 1;
      }
      bfp->values = newMem;

      return 0;
}

int allocate_new_loop(BrainfuckProgram *bfp) {
      byte *newMem = realloc(bfp->loops_starts, bfp->loop_size * sizeof(byte));

      if (!newMem) {
            return 1;
      }
      bfp->loops_starts = newMem;

      return 0;
}

void set_value(BrainfuckProgram *bfp, byte value) {
      int tempValue = value < 0 ? 255: value % 256;
      *(bfp->values + bfp->index) = tempValue;
}

int get_value(BrainfuckProgram *bfp) {
      return *(bfp->values + bfp->index);
}

// FILE functions
int get_file_name(int argc, char *argv[], char** fname) {
     if (argc == 1) {
           printf("File name is missing\n");
           return 1;
     }

     *fname = argv[1];
     return 0;
}

int get_file_size(FILE *file) {
    fseek(file, 0L, SEEK_END);
    int size = ftell(file);
    rewind(file);
    return size;
}

// Main function
int main(int argc, char *argv[]) {
      // Vector part
      BrainfuckProgram bfp = {
            .values = NULL,
            .size = 1,
            .index = 0,

            .loops_starts = NULL,
            .loop_index = 0,
            .loop_size = 0

      };


      initialize(&bfp);

      // FILE Part
    char* fname = NULL;
      int error = get_file_name(argc, argv, &fname);
      if (error) {
            goto exit;
      }

    // Create file variable
    FILE *fin = NULL;
    fin = fopen(fname, "r");

    int size = get_file_size(fin);

    // Create a char array of the right size
    byte *prog = NULL;
    prog = malloc(size * sizeof(*prog));
      // Read 1 byte size times
    fread(prog, 1, size, fin);

    for (int i = 0; i < size; i++) {
            byte current =  *(prog+i);
        switch (current) {
            case INCR:
                        set_value(&bfp, get_value(&bfp) + 1);
                break;

            case DECR:
                        set_value(&bfp, get_value(&bfp) - 1);
                break;

            case FORW:
                        // if index+1 == size then we are at the
                        // last element of the vector cause
                        // size = index - 1
                        if (bfp.index+1 == bfp.size) {
                              bfp.size++;

                              int error = allocate_values(&bfp);
                              if (error) {
                                    printf("an error occured while forwarding pointer");
                                    goto close;
                              }
                        }
                        bfp.index++;

                break;

            case BACK:
                        // bfp.index == 0 then !bfp.index == true
                        if (!bfp.index) {
                             printf("can go back from index 0\n");
                             goto close;
                        }
                        bfp.index--;

                break;

            case SJMP:
                        if (bfp.loop_index == bfp.size) {
                              bfp.loop_size++;

                              int error = allocate_new_loop(&bfp);
                              if (error) {
                                    printf("an error occured while forwarding pointer");
                                    goto close;
                              }

                        }
                        bfp.loop_index++;
                        *(bfp.loops_starts + bfp.loop_index) = i;

                break;

            case EJMP:
                        if (bfp.loop_index == -1) {
                              printf("cannot go back");
                              goto close;
                        }

                        if (get_value(&bfp) != 0) {
                              i = *(bfp.loops_starts + bfp.loop_index);

                        } else {
                              bfp.loop_index--;

                        }

                break;

            case PRTC:
                        printf("%c", get_value(&bfp));

                break;

            case GETC:
                        ; // Semicolon here because of the label error
                        byte buf;
                        scanf("%c", &buf);
                        set_value(&bfp, buf);

                break;

        }
    }

      close:
            fclose(fin);

      exit:
            ;

    return 0;
}

Gostaria de ter alguns conselhos relacionados ao C sobre o que fiz de errado também.

Thomas Teixeira
fonte
11
Acho que o programa é pequeno o suficiente para publicá-lo na sua pergunta, e não como um link para ele.
KamilCuk 18/11/19
11
Percorra seu código em um depurador; ou e / ou peixe com pontos de interrupção enquanto ele está funcionando e, aparentemente, no loop infinito
MM
2
Eu upvote para a utilização de linguagem malloc adequado em vez de repetir o nome do tipo 3 vezes como a maioria das pessoas parecem fazer
MM
3
você pode explicar por que você escreve value < 0 ? 255: value % 256 quando valueé um caracter não assinado?
MM
2
Mandelbrot é computacionalmente caro, um intérprete não é o ideal. "Ficar preso" é indistinguível de "ainda não concluído" quando você não tem idéia real de quanto tempo o programa deve levar. Deixe funcionar durante a noite, durma em idéias para escrever um compilador BF.
Hans Passant

Respostas:

4

O programa Mandelbrot leva um tempo decente para ser executado em um bom intérprete. Seu, bem, não é tão bom. É por isso que "trava" no início.

Algumas boas idéias que você teve, em termos de desempenho:

  • pré-cálculo de loops

Algumas idéias ruins, em termos de desempenho:

  • Usando funções abstratas de alocação em vez de chamar realloc
  • usando scanfe em printfvez de getchareputchar

Algumas coisas estranhas que você fez:

  • Truncando manualmente unsigned charquando a envolvente está implícita
  • verificando se um unsigned charé menor que 0
  • usando *(x + y)sintaxe em vez de indexação de matrizx[y]
  • Eu não acho que você realmente use esses loops pré-calculados, se o fizer, provavelmente não está corretamente
  • Você provavelmente também cria um novo loop pré-calculado toda vez que passa o loop novamente
  • Usando as funções get_valuee em set_valuevez de ponteiros (isso será um problema de desempenho se você não compilar com otimizações)

Algumas sugestões:

  • Tente evitar chamar funções quando não precisar o máximo possível.
  • Escreva um compilador BrainFuck-> C usando esta referência .

Pensando bem, talvez você deva tentar depurar este programa .

Você também pode usar meu intérprete como referência se estiver bem com a GPLv3. Observe que ele não pré-calcula os loops.

SS Anne
fonte
Vou tentar implementar todos os conselhos que você me deu, obrigado! Embora eu tenha uma pergunta, o que você quer dizer com loop pré-calculado?
Thomas Teixeira
11
@ThomasTeixeira allocate_new_loope loop_index?
SS Anne Anne