Function of free() in abstract types

Asked

Viewed 63 times

-1

I expected the following code to record the students' data, and then immediately erase it from memory:

testTurma. c

#include "aluno.h"
#include <stdlib.h>

int main(){
    aluno *turma;
    turma = malloc(3*sizeof(aluno));

    turma[0] = regAluno("Joao",123,7.0,5.0);
    turma[1] = regAluno("Maria",124,8.0,6.0);
    turma[2] = regAluno("Jose",163,6.0,5.0);

    for(int i = 0;i<3;i++) exibeAluno(turma[i]);

    free(turma);

    //Aqui eu esperava ou exibir lixo de memória, ou dar erro e abortar
    for(int i = 0;i<3;i++) exibeAluno(turma[i]); 

    return 0;
}

But I get the following output:

Nome: Joao,   Matr.: 123,   Media: 6.00
Nome: Maria,   Matr.: 124,   Media: 7.00
Nome: Jose,   Matr.: 163,   Media: 5.50
Nome: (null),   Matr.: 123,   Media: 6.00
Nome: Maria,   Matr.: 124,   Media: 7.00
Nome: Jose,   Matr.: 163,   Media: 5.50

That is, it only erased the first property of the student struct that was reserved in the first memory space, someone could explain to me how the free() function actually works, and how I can free up all allocated memory space?

Rest of the code:

pupil. c

#include "aluno.h"
#include <stdlib.h>
#include <stdio.h>

aluno regAluno(char *nome,int matricula, float nota1,float nota2){
    aluno a;
    a.nome = nome;
    a.matricula = matricula;
    a.nota1 = nota1;
    a.nota2 = nota2;
    a.media = (nota1+nota2)/2;

    return a;
}

aluno *criaPontAlunos(int size){
    aluno *p;
    p = malloc(size*sizeof(aluno));

    return p;
}

void exibeAluno(aluno a){
    printf("Nome: %s,   Matr.: %d,   Media: %.2f\n",
    a.nome, a.matricula, a.media);
}

pupil. h

#ifndef ALUNO_H
#define ALUNO_H

typedef struct{
    char *nome;
    int matricula;
    float nota1;
    float nota2;
    float media;

} aluno;

aluno regAluno(char *nome,int matricula, float nota1,float nota2);
aluno *criaPontAlunos(int size);
void exibeAluno(aluno a);


#endif
  • Could show the structure of the student struct?

  • According to the link insert link description here It was enough to perform the free (class).

  • You need to see what you’re up to because this is really wrong, to give you a solution just knowing you need it, it’s not just a mistake in free().

  • @G.Bittencourt typedef struct{ char *name; int matricula; float Nota1; float nota2; float media; } student;

2 answers

5

I believe there are more problems in this code - without seeing the function regAluno you can’t tell exactly what (all of a sudden it works).

But about the function free: she brand name the memory as free for the operating system - including for parts of your own program that are "piloted" by code snippets created by the C compiler and the libraries used - you have no control over them. The chance is that this is what is happening: the memory is re-used by its own program (remembering, automatically), to allocate the variable itself i of the second for. Declare the int i at the beginning of the function instead of at each for and you should see a change in behavior there.

But what’s important for you to remember it doesn’t do is "zero" the area of the memory released: all your data remains in the same place, except a marker right at the beginning there. So, programs that have sensitive data in memory- especially if they are going to run on a shared server, you have to be careful to explicitly reset these sensitive data before releasing the memory. This can be a lot of work, since in "real" application languages for the web and database, you have little direct control over memory, as it is possible to have in C. (And anyway, the flaws in the current CPU architecture with the name of "Spectre" and "Meltdown" showed that even with the running program and theoretically exclusive access to memory, these data can leak: security is a thankless job).

Yes - that program could give error for "access violation" - and it is likely that if you insist on running something like this, you will get an error like this once in a while. But the operating system did not dedicate this allocated area to another process immediately, and did not give an "access failure" - in this case. Interesting for you to see that the data remains in memory.

At the discretion of several factors including: your operating system, and the compiler, may be that small chunks of memory are reused within the same process space, hence you do not have this error occurring. But the reason this isn’t documented more transparently is simply that you should not use this memory after free.

So: yes - congratulations on the initiative of performing the experiment, and ask when you did not understand. And yes, the correct answer remains the theoretical answer: this is wrong, and the chance of the program suddenly stopping is too great.

(If you had put the complete code of the program I would compile and run here - as it is a different system than yours, probably the result would be an access violation and termination of the program).

  • thanks for the great reply, could tell me how I can put code format in the comments?

  • I believe it is not possible to comment on formatted codes, but you can edit your post.

  • done, at first I was using the function childPontAlunos() to allocate, but with the problem I tried direct, which did not change much

-2

Nelson,

do the following test:

for(int i = 0;i<3;i++)
{
    free(turma[i]);
}

for(int i = 0;i<3;i++) 
    exibeAluno(turma[i]);
  • I did this as soon as I saw the bug and did not compile --- testeTurma. c:14:10: error: incompatible type for argument 1 of ːfree' free(class[0]);

  • class[0] is not a pointer, so you cannot use the free( ).

Browser other questions tagged

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