Perché il mio programma continua a bloccarsi durante l'esecuzione del programma mandelbrot brainf ***?


9

Volevo migliorare le mie abilità in C, quindi cerco alcune idee del programma.

Qualcuno propone di creare un semplice interprete Brainf *** e quindi un compilatore. Quindi eccomi qui.

Ho creato l'interprete e funziona come previsto, tranne che con il programma Mandelbrot:

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

Non capisco perché, ma il mio programma continua a rimanere bloccato in una sorta di ciclo infinito. Provo a eseguire il debug usando gdb ma è difficile trovare il punto di interruzione giusto per capire perché si è bloccato.

Il programma è il seguente:

#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;
}

Mi piacerebbe avere qualche consiglio in C su ciò che ho fatto di sbagliato.


1
Penso che il programma sia abbastanza piccolo da postarlo nella tua domanda, piuttosto che un link ad esso.
KamilCuk,

1
Scorri il tuo codice in un debugger; e / o pesce con punti di interruzione mentre è in esecuzione e apparentemente nel ciclo infinito
MM

2
Ho votato per aver usato il linguaggio idioma corretto invece di ripetere il nome del tipo 3 volte come la maggior parte della gente sembra fare
MM

3
puoi spiegare perché scrivi value < 0 ? 255: value % 256 quando valueè un carattere non firmato?
MM

2
Mandelbrot è costoso dal punto di vista computazionale, un interprete non è l'ideale. "Rimanere bloccato" è indistinguibile da "non ancora fatto" quando non si ha idea di quanto tempo dovrebbe durare il programma. Lasciatelo correre tutta la notte, dormite sulle idee per scrivere un compilatore BF.
Hans Passant,

Risposte:


4

Il programma Mandelbrot impiega un tempo decente per funzionare su un buon interprete. Tuo, beh, non è così bello. Ecco perché "si blocca" all'inizio.

Alcune buone idee che hai avuto, per quanto riguarda le prestazioni:

  • anelli precalcolanti

Alcune cattive idee, dal punto di vista delle prestazioni:

  • Utilizzo delle funzioni di allocazione astratte invece di chiamare realloc
  • usando scanfe printfinvece di getchareputchar

Alcune cose strane che hai fatto:

  • Troncare manualmente un unsigned charquando avvolgente è implicito
  • controllando se an unsigned charè inferiore a 0
  • usando la *(x + y)sintassi invece dell'indicizzazione dell'arrayx[y]
  • Non penso che tu effettivamente usi quei loop precalcolati, se lo fai probabilmente non correttamente
  • Probabilmente si crea anche un nuovo ciclo precalcolato ogni volta che si ripassa il ciclo
  • Utilizzo delle funzioni get_valuee set_valueanziché dei puntatori (questo sarà un problema di prestazioni se non si compila con ottimizzazioni)

Un paio di suggerimenti:

  • Cerca di evitare di chiamare le funzioni quando non è necessario il più possibile.
  • Scrivi un compilatore BrainFuck-> C usando questo riferimento .

A pensarci bene, forse dovresti provare a eseguire il debug di questo programma .

Puoi anche usare il mio interprete come riferimento se stai bene con GPLv3. Si noti che non precalcola i loop.


Cercherò di attuare tutti i consigli che mi hai dato, grazie! Anche se ho una domanda, cosa intendi con ciclo precalcolato?
Thomas Teixeira,

1
@ThomasTeixeira allocate_new_loope loop_index?
SS Anne,
Utilizzando il nostro sito, riconosci di aver letto e compreso le nostre Informativa sui cookie e Informativa sulla privacy.
Licensed under cc by-sa 3.0 with attribution required.