Recursion in C - The crash program

Asked

Viewed 64 times

2

The goal of the program is to ask the user for two digits and generate a list of sequences, each containing information from the previous.

For example:

User writes 1 1 and the generated list is:

  • 21, because the previous sequence has two 1.
  • 1211, because the previous sequence has "one" 2 and "one" 1.
  • 3112, because the previous sequence has "three" 1 and "one" two
  • etc..

When I try to run the show, crash on the 50th line and I have no idea why.

I appreciate the help and I apologize for the possible irrelevance of the issue.

Source:

#define Max_num   5000

typedef struct {
    int numero;
    int count;
}Numero;

typedef Numero Sequencia[Max_num];

// retorna comprimento da sequencia
int add_num ( Sequencia numeros , int n , int num){

    if ( n == 0) {
        numeros[0].numero = num;
        numeros[0].count = 1; 
        return n+1;

    }
    int i;
    for ( i = 0 ; i < n ; i++){

        if ( numeros[i].numero == num){
            numeros[i].count++;
            return n;
        }
    }

    numeros[n].numero = num;
    numeros[n].count = 1;
    return n+1;

}
void print_vetor(int vetor[] , int n){
    int i;
    for ( i = 0 ; i < n; i++){
        printf("%d",vetor[i]);
    }
    printf("\n");

}
void ler_sequencia(Sequencia numeros , int vetor[], int n){

    int i; //controla vetor recetor

    int x = 0;//controla sequencia
    for ( i = 0 ; i < n; i++){
        vetor[i] = numeros[x].count;
        i++;
        vetor[i] = numeros[x].numero;
        x++;

    }

}
void processar_vetor( int vetor[] , int n ){

        int i;
        Sequencia numeros;
        int c = 0;// comprimento da sequencia
        for ( i = 0 ; i < n; i++){
            c = add_num( numeros , c , vetor[i]);
        }
        int vetor_final[2*c];
        ler_sequencia( numeros , vetor_final , 2*c);
        print_vetor(vetor_final , 2*c);

        processar_vetor( vetor_final, 2*c);
    }


int main(){

    int vetor[2];
    printf("Escreve dois algarismos: ");
    scanf( "%d %d",&vetor[0] ,&vetor[1]);
    processar_vetor( vetor , 2 , 1);
    return 0;
}
  • I’m going to say something that I’m almost sure will be ignored for some reason. The problem with this algorithm is that it’s not suitable for recursion. Then it gets tricky to get it right. I know it may be that someone sent it, that it may be that they are doing it to learn, but the ideal is to learn to do something that is inherently recursive and not force the bar on something that is not. See http://answall.com/q/21551/101 and http://answall.com/q/107657/101

  • I understand , but I would like to understand what is the real problem with the code I wrote, why does it only work until the 50th recursion?

  • (1) I think that this program neither compiles, the declaration of "parse_vector" is different from the use (2) the function "parse_vector" need to have a stop criterion, otherwise it is in infinite loop and will crash even

1 answer

1

As @José X commented, every recursion program must have a stop criterion. In your case the problem is that you are statically allocating infinite integer vectors called "numbers" according to their variable, one time you will have allocated so much memory that it will "invade" the space of another program and the OS will stop the same.

To fix you must or create a stop, in your case grotesquely but easily, could make the function parse_vector return an integer and limit the number of iterations by checking each call of the same if that number reached the limit, or if you really want the program to run indefinitely until a Kill signal is sent by the user you must dynamically allocate the memory of the new vector to each iteration of the parse_vector function and at the end you must give free in the vector that will no longer be used.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.