Accessing the allocation causes the program to stop working

0

I have a problem accessing a struct of subscribers

typedef struct Inscrito {
     char nome[50];
     float cadastro;
     float nota;
};

Inscrito *inscritos = NULL;

It is a global variable and every time I access it in some way (either to check for an error or to put the values inside it after it has been allocated the error)

(data is the variable of the .txt file I opened)

int Conta_MaioresNotas(FILE *Dados){

float NotaMin;
char info[20];
float numero;
int contador = 0, i = 0;

printf("Voce escolheu as maiores notas\n");
printf("Apartir de qual nota voce quer? ");
scanf("%f", &NotaMin);

while(!feof(Dados)){
    fscanf(Dados,"%s", &info);
    numero = atof(info);
    if(numero && numero > NotaMin && numero < 1001){
        contador++;
        printf("%.2f\n", numero);
    }
}
printf("%d\n", contador);
VoltaInicio(Dados);


inscritos = (Inscrito *) malloc(contador*sizeof(Inscrito));

if(inscritos == NULL) {printf("Deu erro meu amigo\n"); exit(-1);}


 while(!feof(Dados)){
    fscanf(Dados,"%s ", &info);
    numero = atof(info);
    if(!numero){
        strcat(inscritos[i].nome, " ");
        strcat(inscritos[i].nome, info);
        }
    else{
        if(numero > 1000){
            inscritos[i].cadastro = numero;

            }
        else{
            if(numero > NotaMin){
                inscritos[i].nota = numero;
                i++;
                }
            else{
                strcpy(inscritos[i].nome,"");
                inscritos[i].cadastro = 0.0;
                }
            }
        }    
}
VoltaInicio(Dados);

This while giving the error, but I do not understand why, the program of the stick and closes "The program stopped working. A problem caused the program to stop working"

/*printf("Cadatrados filtrados:\n");
for(i = 0; i < contador; i++)
{
    printf("Nome: %s\nCadastro: %f\nNota: %.2f\n\n", inscritos[i].nome, inscritos[i].cadastro, inscritos[i].nota);
}*/


return contador;
}

I'm not sure what to do,

(already tried to pass the subscribed pointer to the function and gave the same error)

    
asked by anonymous 07.04.2018 / 21:20

1 answer

0

As the code was presented, it's a bit difficult to determine with certainty what the problem is, why it was not fully informed, but there are some considerations.

First, I do not encourage the use of global variables. I understand the ease this provides but can be responsible for these errors if you do not pay attention to what you do. It is important to note that global variables can be changed anywhere, so it is very easy for you to end up releasing or overwriting global variable data through an unforeseen execution flow. Develop your software with in mind the ways in which the code can run. Make your functions just one task, not functions that do everything that is to be done.

Regarding the code presented, I first believe that you did not copy directly from what you did because there is an error in the struct statement when using this struct notation with typedef (which is commendable, I appreciate the practice as you did), it is necessary to define the new name after the struct declaration ends before the semicolon in its example :

typedef struct Instrucao{
  ...
} Instrucao; //<---Erro

That's why the example provided would no longer work, but I do not think that's the case.

I also do not believe this is the case of memory release, but keep in mind that you should release the memory allocated in the instrucoes variable using the free () due to the dynamic allocation made.

As for your specific error, you should also note that within the While you suspect you have an error, you use the strcat function, and it, as the name says , performs a concatenation of strings. Initially the name in question is empty, it has absolutely nothing, so there is a small problem in wanting to concatenate a string to nothing, the value is literally empty , but most compilers will not complain about it superficially, if you use an execution parser like valgrind , you will notice that it will complain about some errors related to memory-dependent instructions that have not yet been initialized (your string before concatenating anything).

This concatenation is also dangerous because the name variable in your Instruction was declared as having a maximum length of 50 characters, so it is possible that some of the concatenations you is performing over 50 characters, which means that one of the names has more than 50 characters, or an undue concatenation is occurring and exceeding the limit of characters you have allocated, causing what is called a stack smashing , you're basically using more characters than you've informed.

If the problem is not in the name in the instructions, it may be in the info character vector, which supports only 20 characters, so one of the first or last names can exceed 20 characters and cause the error informed.

It turns out that not always exceeding the size you've allocated will cause an error like this, what happens is undefined behavior or undefined behavior , sometimes it can work, at times not times, will vary depending on the execution of your program and therefore will not be relied upon to use it. It is important that you change these strings to a dynamic allocation or increase their size to see if that resolves the problem in question.

    
08.04.2018 / 03:42