-
Notifications
You must be signed in to change notification settings - Fork 0
/
demo-valgrind.c
117 lines (87 loc) · 2.07 KB
/
demo-valgrind.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
struct person {
int id;
char *name;
};
struct node {
struct node *next;
struct node *prev;
struct person *p;
};
struct node *create_person (int id, char *name)
{
struct node *n = malloc (sizeof (struct node));
struct person *p = malloc (sizeof (struct person));
p->id = id;
p->name = strdup(name); /* Possible leak, if we don't free name */
n->prev = NULL;
n->next = NULL;
n->p = p;
return n;
}
void add_person(struct node *list, struct node *person)
{
struct node *tail = list;
if (tail == NULL) {
fprintf(stderr, "NULL list pointer sent to add_person\n");
exit(1);
}
while(tail->next != NULL)
tail = tail->next;
tail->next = person;
person->prev = tail;
tail = person;
}
void delete_person (struct node *n)
{
struct node *prev = n->prev;
struct node *next = n->next;
if (prev == NULL && next == NULL ) {
;
} else if (prev == NULL) {
next->prev = NULL;
} else if (next == NULL) {
prev->next = NULL;
} else {
next->prev = n->prev;
prev->next = n->next;
}
free(n->p->name);
free(n->p);
free(n);
n = NULL;
return;
}
void write_list (struct node *list)
{
for(;list;list = list->next) {
printf("id = %d\nName = %s\n\n", list->p->id, list->p->name);
}
return;
}
int main (void)
{
struct node *list = NULL, *n;
list = create_person (1, "Dave Neary");
n = create_person (2, "Thomas Perl");
add_person (list, n);
n = create_person (3, "Alison Chaiken");
add_person (list, n);
n = create_person (4, "Andrea Grandi");
add_person (list, n);
n = create_person (5, "Kevin Ottens");
add_person (list, n);
for (n=list; n; ) {
struct node *next = n->next;
if (n->p->id % 2 == 0) {
delete_person(n);
}
n = next;
}
n = create_person (6, "Bob Spencer");
add_person (list, n);
write_list (list);
return EXIT_SUCCESS;
}