-
Notifications
You must be signed in to change notification settings - Fork 0
/
pointers.c
81 lines (73 loc) · 1.65 KB
/
pointers.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
/*
Funções relacionadas com alocação de memória
*/
#include <stdio.h>
#include <stdlib.h>
#include "pointers.h"
/**
* malloc com com verificacao de sucesso incluida
*/
void *safeMalloc(size_t _size){
void *p = malloc(_size);
if (p == NULL){
exit (0);
}
return p;
}
/**
* chama as funcoes que libertam mem. alocada
* para as estruturas onde o ficheiro de input
* foi carregado
*/
void freeAll( int _num_tur_points, int ***tur_points,
int _height, int ***map){
freeTurPoints(_num_tur_points, tur_points);
freeCostMap(map, _height);
}
/**
* liberta mem. allocada paraa tabela dos custos
*/
void freeCostMap(int ***map, int _height){
for (int row = 0; row < _height; row++)
free((*map)[row]);
free(*map);
*map = NULL;
}
/**
* liberta mem allocada para a tabela dos pontos a visitar
*/
void freeTurPoints(int _num_tur_points, int ***tur_points){
for (int point_ix = 0; point_ix < _num_tur_points; point_ix++)
free((*tur_points)[point_ix]);
free(*tur_points);
*tur_points = NULL;
}
/**
* liberta a mem allocada de um no do hipergrafo
*/
void freeHyperNode(HyperNode n, int num_tur_points){
for (int i = 0; i < num_tur_points; i++){
if (n.edges[i] != NULL)
freePath(n.edges[i]->path);
free(n.edges[i]);
}
free(n.edges);
}
/**
* liberta memoria alocada para um caminho
*/
void freePath(Path *p){
if (p == NULL)
return;
Path *next = p->next;
free(p);
freePath(next);
}
/**
* liberta a mem alocada para o hipergrafo
*/
void freeGraph(HyperNode *graph, int _num_tur_points){
for (int i = 0; i < _num_tur_points; i = i + 1)
freeHyperNode(graph[i], _num_tur_points);
free(graph);
}