Recursion in C - The crash program

1

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

For example:

The user writes 1 1 and the generated list is:

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

When I try to run the program, it crashes into the 50th line and I have no idea why.

Thank you for your help and I apologize for any irrelevance.

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;
}
    
asked by anonymous 23.12.2016 / 19:51

1 answer

0

As José @ commented, every program in recursion should have a stop criterion. In your case the problem is that you are statically allocating infinite vectors of integers called "numbers" according to their variable, one hour 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 should either create a stop, in your case in a grotesque but easy way, could make the function process_vector return an integer and limit the number of iterations by checking each call of the same if that number has 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 process_array function and at the end you should give free to the vector that will no longer be used. >     

28.12.2016 / 07:59