0

I'm trying to run the following code on C, using Xcode, and it's returning an erro that I can't figure out, any clue on it? Below is the code and a print of the Xcode Screen.

It runs mostly the code, until I select the number of duplicate entries it should remove.

Error Returned

    #include<stdio.h>
#include <stdlib.h>
//#include<stdlib.h>
#include<math.h>
#include <string.h>
#include <stdbool.h>
#define TRUE 1
#define FALSE 0
int combinacoes(int n, int r);
void filtro_75_5();
void filtro_60_6();
void filtro_49_7();
void filtro_60_8();
void filtro_60_9();
void filtro_geral();

int** Filtro;
int npainel;
int ndezenas;
int* dezenas;
int** Base = 0;
bool verificaRepeticoes = true;
int total_registros;
int repeticao = 0;
int total_registros_r = 0;
FILE *f;


int main()
{

    printf("Digite o Painel: ");
    scanf("%d",&npainel);

    printf("Digite a Dezena: ");
    scanf("%d",&ndezenas);

    int resultado;
    resultado = combinacoes(npainel,ndezenas);
    printf("%d",resultado);

    int iniciar;
    printf("\nDeseja gerar as combinacoes [1] Sim [2] Nao");
    scanf("%d", &iniciar);

    int usaragrupamento;
    printf("\nUsar agrupamento de Dezenas [1] Sim [2] Nao]");
    scanf("%d", &usaragrupamento);

    int filt_repeticoes;
    printf("\nUsar filtro de Qtd de repeticoes no Total de:");
    scanf("%d", &filt_repeticoes);

    if (iniciar ==1)
    {

        f = fopen( "/Volumes/BANCO/combinacoes.txt" , "w" );


        int rows = 2;
        int cols = 5;

        int** Filtro = calloc(rows, sizeof *Filtro); // I prefer calloc unless i really am reallocating

        for (int i = 0 ; i < rows ; ++i)
        {
            Filtro[i] = calloc(cols, sizeof **Filtro);
        }

        for (int i = 0 ; i < rows ; ++i)
        {
            free(Filtro[i]);
        }
        free(Filtro);

        /*redim dezenas(ndezenas-1) //Vetor de cada dezena do conjunto*/
        dezenas = (int *)realloc(dezenas, (ndezenas-1) * sizeof(int *));

        /*redim Base(-1,-1)*/
        Base = (int **) realloc(Base,(-1) * sizeof(*Base));

        /*redim Base(1000,ndezenas-1)*/
        Base = (int **) realloc(Base,(1000) * sizeof(*Base));

        if (usaragrupamento == 1)
        {

            switch(ndezenas){

                case 5:
                    filtro_75_5();
                    break;
                case 6:
                    filtro_60_6();
                    break;
                case 7:
                    filtro_49_7();
                    break;
                case 8:
                    filtro_60_8();
                    break;
                case 9:
                    filtro_60_9();
                    break;

            }
        }else{

            filtro_geral();
        }


    }

    return 0;
}

int combinacoes(int n, int r)
{
    int combinacao;

    //combinacao = 0;
    if (n<1 || r <1)
    {

        combinacao = 0;
    }else{

        double u;
        double v;
        int w;
        int t;

        u=1;
        t=1;

        //for(t=1;u=u*t;
        while(t<=n)
        {
            u=u*t;
            t++;
        }
        //calcula o fatorial de n(!n)
        v= 1;
        t= 1;
        if(n>r)
        {
            while(t<=n-r)
            {
                v = v*t;
                t++;
            }
        }
        w = 1;
        t = 1;

        //Calcula Fatorial de n-r (n-r)!
        while(t<=r)
        {
            w=w*t;
            t++;
        }

        combinacao = round(u/(v*w));
        return combinacao;
    }

    return combinacao;
}

void filtro_49_7()
{

    Filtro[0][0]=1;
    Filtro[0][1]=7;

    Filtro[1][0]=8;
    Filtro[1][1]=14;

    Filtro[2][0]=15;
    Filtro[2][1]=21;

    Filtro[3][0]=22;
    Filtro[3][1]=28;

    Filtro[4][0]=29;
    Filtro[4][1]=35;

    Filtro[5][0]=36;
    Filtro[5][1]=42;

    Filtro[6][0]=43;
    Filtro[6][1]=49;

}

void filtro_60_6(){

    Filtro[0][0]=1;
    Filtro[0][1]=30;

    Filtro[1][0]=1;
    Filtro[1][1]=30;

    Filtro[2][0]=1;
    Filtro[2][1]=30;

    Filtro[3][0]=31;
    Filtro[3][1]=60;

    Filtro[4][0]=31;
    Filtro[4][1]=60;

    Filtro[5][0]=31;
    Filtro[5][1]=60;

}

void filtro_60_8(){
    Filtro[0][0]=1;
    Filtro[0][1]=15;
    Filtro[1][0]=1;
    Filtro[1][1]=15;

    Filtro[2][0]=16;
    Filtro[2][1]=30;
    Filtro[3][0]=16;
    Filtro[3][1]=30;

    Filtro[4][0]=31;
    Filtro[4][1]=45;
    Filtro[5][0]=31;
    Filtro[5][1]=45;

    Filtro[6][0]=46;
    Filtro[6][1]=60;
    Filtro[7][0]=46;
    Filtro[7][1]=60;
}

void filtro_60_9(){
    Filtro[0][0]=1;
    Filtro[0][1]=20;

    Filtro[1][0]=1;
    Filtro[1][1]=20;

    Filtro[2][0]=1;
    Filtro[2][1]=20;

    Filtro[3][0]=21;
    Filtro[3][1]=40;

    Filtro[4][0]=21;
    Filtro[4][1]=40;

    Filtro[5][0]=21;
    Filtro[5][1]=40;

    Filtro[6][0]=41;
    Filtro[6][1]=60;

    Filtro[7][0]=41;
    Filtro[7][1]=60;

    Filtro[8][0]=41;
    Filtro[8][1]=60;
}

void filtro_75_5(){

    Filtro[0][0]=1;
    Filtro[0][1]=15;
    Filtro[1][0]=16;
    Filtro[1][1]=30;
    Filtro[2][0]=31;
    Filtro[2][1]=45;
    Filtro[3][0]=46;
    Filtro[3][1]=60;
    Filtro[4][0]=61;
    Filtro[4][1]=75;
}

void filtro_geral()
{
    int x;
    int n;
    int r;

    r=ndezenas;
    n=npainel;
        x=1;

        while(x<=r-1)
        {
            Filtro[x][0]=1;
            Filtro[x][1]=n;
            x++;
        }

}

void gerar_dezenas(int npainel, int ndezenas, int inicio,int termino, int nivel)
{

    int w,unidade, xx,igual;
    char *sszero = NULL;//, sgeral[1];
    char *sgeral = NULL;
    bool duplicou = false;

    unidade = inicio;

    while(unidade<=termino+nivel)
    {

        if (unidade>=Filtro[nivel-1][0] && unidade<=Filtro[nivel-1][1])
        {
            dezenas[nivel-1] = unidade;

            if (nivel<ndezenas)
            {
                gerar_dezenas(npainel,ndezenas,unidade+1,npainel-ndezenas,nivel+1);
            }else{
                sgeral = (char *)realloc(sgeral, 1 * sizeof(char *));
                //sgeral[0] = '';
                duplicou=false;

                //xojo
                if (verificaRepeticoes ==true){
                    xx = total_registros-1;

                    while(xx>=1)
                    {
                        igual =0;

                        w=0;

                        while(w<ndezenas-1)
                        {
                            if(Base[xx][w] == dezenas[w])
                            {
                                igual=igual+1;
                            }
                            w++;
                        }

                        if (igual>(ndezenas-repeticao))
                        {
                            duplicou = true;
                            return;
                        }

                        xx--;
                    }
                }

                //////Adiciona um conjunto a Base
                if (duplicou==false || total_registros==0){

                    w = 0;

                    while (w < ndezenas-1){

                        if (dezenas[w]<10){
                            sszero = (char *)realloc(sszero, 2 * sizeof(char *));
                            sszero[0]='0';
                        }
                        /*else{
                         sszero = (char *)realloc(sszero, 1 * sizeof(char *));
                         sszero[0]="";
                         }*/

                        /////So alimenta vetor se for para verificar repeticoes
                        if (verificaRepeticoes==true){
                            Base[total_registros][w]=dezenas[w];
                        }

                        int tam_sgeral = sizeof(sgeral);
                        sgeral = (char *)realloc(sgeral, (sizeof(sgeral) + sizeof(sszero) + sizeof(dezenas[w]))* sizeof(char *));

                        int z;
                        z=0;

                        while(z<sizeof(sszero))
                        {
                            sgeral[tam_sgeral+z] = sszero[z];
                        }

                        tam_sgeral = tam_sgeral + z;
                        z=0;

                        char str[2];
                        sprintf(str, "%d", dezenas[w]);
                        while(z<sizeof(dezenas[w]))
                        {
                            sgeral[tam_sgeral+z] = str[z];
                            //strcpy(sgeral[tam_sgeral+z],str[z]);
                        }

                        w++;
                    }

                    total_registros=total_registros+1;
                    //int** Base2;
                    if (total_registros % 1000 == 0)
                    {
                        Base = (int **) realloc(Base,(total_registros+1000) * sizeof(*Base)); //Vetor total com os conjuntos de combinacoes
                        /*TfGerados.text=str(total_registros);
                         TfGerados.refresh;*/
                        printf("%s",&"Gerados: " [ total_registros]);
                    }

                    if (f != NULL)
                    {
                        printf("Teste");
                        fwrite(sgeral , 1 , sizeof(sgeral) , f);

                    }
                }
                else{

                    total_registros_r=total_registros_r+1;

                    if (total_registros_r % 10000 == 0){
                        printf("%s", &"Jogados Fora: " [ total_registros_r]);
                        /*TfJogadosFora.text=str(total_registros_r);
                         TfJogadosFora.refresh*/
                    }

                }

            }
        }
        else{

            total_registros_r=total_registros_r+1;

            if (total_registros_r % 10000 == 0){
                printf("%s", &"Jogados Fora: " [ total_registros_r]);
                /*TfJogadosFora.text=str(total_registros_r)
                 TfJogadosFora.refresh*/
            }
        }
    }

}
Diego Patrocinio
  • 100
  • 1
  • 4
  • 13

1 Answers1

2

The variable Filtro is not initialised properly. Filtro is a block of pointers to int but after the realloc all of those pointers are dangling. Once you have allocated some memory to Filtro you must loop through for each element in Filtro and alloc the space to hold the ints.

This is what you have assuming ndezenas-1 is 2.

Filtro -->+-----+
          | int*|---> ???
          +-----+
          | int*|---> ???
          +-----+

This is what you need

Filtro -->+-----+     +------------ 
          | int*|---> |int|int|int| .... etc 
          +-----+     +------------
          | int*|-+ 
          +-----+ |   +------------ 
                  +-> |int|int|int| .... etc 
                      +------------

Here's an example of how to do it

int rows = 2;
int cols = 5;

int** filtro = calloc(rows, sizeof *filtro); // I prefer calloc unless i really am reallocating

for (int i = 0 ; i < rows ; ++i)
{
    filtro[i] = calloc(cols, sizeof **filtro);
}

Also to free the memory at the end

for (int i = 0 ; i < rows ; ++i)
{
    free(filtro[i]);
}
free(filtro);

Edit

My example was just an example. You weren't meant to copy it verbatim.

The big problem now is that you have now declared Filtro twice, once as a global variable and once as a local variable to main. This means the global version never gets any memory allocated to it. You do not need to redeclare Filtro when you do the calloc.

filtro = calloc(rows, sizeof *filtro); 

The second big problem is that you are freeing the memory straight away. You don't do that until after you have finished using it.

JeremyP
  • 84,577
  • 15
  • 123
  • 161