这些是我定义的结构(不介意语言):
typedef struct cliente
{
char* primeiroNome;
char* ultimoNome;
}Cliente;
typedef struct data
{
int dia;
int mes;
int ano;
}Data;
typedef struct reserva
{
Data* data;
int numReservas;
struct cliente* cliente;
struct reserva* next;
}Reserva;
然后我创建了一个列表:
Reserva* novaListaReservas() // in English the name would be something like newReservationsList()... I guess
{
Reserva* lista = (Reserva*)malloc(sizeof(Reserva));
lista->next = NULL;
lista->numReservas = 0;
return lista;
}
然后在某些功能中我做到了这一点(不完全是因为例如假设它):
Reserva* reserva = novaListaReservas();
Cliente* cliente = (Cliente*)malloc(sizeof(Cliente));
Data* data = (Data*)malloc(sizeof(Data));
data->dia = 30;
data->mes = 4;
data->ano = 2014;
cliente->primeiroNome = (char*)malloc(15 * sizeof(char));
cliente->ultimoNome = (char*)malloc(15 * sizeof(char));
reserva->data = data;
reserva->cliente = cliente;
/*stuff done here/*
//End of Function
现在我在其他一些函数中只有(Reserva * reserva)作为我试过的参数:
free(reserva->cliente->primeiroNome);
free(reserva->cliente->ultimoNome);
free(reserva->cliente);
free(reserva->data);
但我不认为它有效,因为在此之后我仍然可以访问预备数据> data-> dia / mes / ano ...
我解放的方式有什么问题? 有没有办法检查免费“工作”?
答案 0 :(得分:3)
您仍然可以访问释放的内存,但它是未定义的行为。它可能仍然包含" old"数据,或者它现在可能包含完全虚假的东西。