Skip to content

Commit bbf2cd3

Browse files
committed
Fix: Memory Leak when delete person(node) from contacts(linked-list)
1 parent 2f2b400 commit bbf2cd3

File tree

2 files changed

+107
-77
lines changed

2 files changed

+107
-77
lines changed

c/AddressBook/main.c

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// fishc
22
// Code Check is required bcs DataEraser did't do that
3+
#define _CRT_SECURE_NO_WARNINGS
34
#include <stdio.h>
45
#include <stdlib.h>
56
#include <string.h>
@@ -124,10 +125,12 @@ struct Person *findPerson(struct Person *contacts)
124125
{
125126
printf("Name:\n%s\n", current->name);
126127
printf("Phone number:\n%s\n", current->phone);
128+
// Maybe this should show in return value instead
127129
}
128130
else
129131
{
130132
printf("Not Found\n");
133+
// Maybe this should show in return value instead
131134
}
132135

133136
return current;
@@ -138,13 +141,16 @@ void changePerson(struct Person *contacts)
138141
if (current != NULL)
139142
{
140143
printf("Please input new Phone number:\n");
144+
// Maybe this should be wrote in main function instead
141145
scanf("%s", current->phone);
142146
// Bounds Check Elimination is required
143147
printf("Done");
148+
// Maybe this should show in return value instead
144149
}
145150
else
146151
{
147152
printf("Not Found\n");
153+
// Maybe this should show in return value instead
148154
}
149155
}
150156
void delPerson(struct Person **contacts)
@@ -160,15 +166,18 @@ void delPerson(struct Person **contacts)
160166
if (current == person)
161167
{
162168
*contacts = current->next;
163-
// the first struct is target
169+
free(current);
170+
// target is the first struct
164171
}
165172
else
166173
{
167174
while (current->next != person)
168175
{
169176
current = current->next;
170177
}
178+
struct Person *target = current->next;
171179
current->next = current->next->next;
180+
free(target);
172181
}
173182
}
174183
}

c/Library/main.c

Lines changed: 97 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -1,95 +1,116 @@
11
// fishc
2+
#define _CRT_SECURE_NO_WARNINGS
23
#include <stdio.h>
34
#include <stdlib.h>
45
#define MAX_SIZE 100
56

6-
struct Date {
7-
short year;
8-
short month;
9-
short day;
7+
struct Date
8+
{
9+
short year;
10+
short month;
11+
short day;
1012
};
11-
struct Book {
12-
char title[128];
13-
char author[40];
14-
float price;
15-
struct Date date;
16-
char publisher[40];
13+
struct Book
14+
{
15+
char title[128];
16+
char author[40];
17+
float price;
18+
struct Date date;
19+
char publisher[40];
1720
};
1821
void getInput(struct Book *book);
1922
void printBook(struct Book *book);
20-
void InitLibrary(struct Book *Library[]);
21-
void printLibrary(struct Book *Library[]);
22-
void releaseLibrary(struct Book *Library[]);
23-
void getInput(struct Book *book) {
24-
printf("Book Title:\n");
25-
scanf("%s", book->title);
26-
printf("Book Author:\n");
27-
scanf("%s", book->author);
28-
printf("Book Price:\n");
29-
scanf("%f", &(book->price));
30-
printf("Book Publish Date(2000-12-1):\n");
31-
scanf("%hd-%hd-%hd", &(book->date.year), &(book->date.month),
32-
&(book->date.day));
33-
printf("Book Publisher:\n");
34-
scanf("%s", book->publisher);
23+
void InitLibrary(struct Book *library[]);
24+
void printLibrary(struct Book *library[]);
25+
void releaseLibrary(struct Book *library[]);
26+
void getInput(struct Book *book)
27+
{
28+
printf("Book Title:\n");
29+
scanf("%s", book->title);
30+
printf("Book Author:\n");
31+
scanf("%s", book->author);
32+
printf("Book Price:\n");
33+
scanf("%f", &book->price);
34+
printf("Book Publish Date(2000-12-1):\n");
35+
scanf("%hd-%hd-%hd", &book->date.year, &book->date.month, &book->date.day);
36+
printf("Book Publisher:\n");
37+
scanf("%s", book->publisher);
3538
}
36-
void printBook(struct Book *book) {
37-
printf("Book Title:\n%s\n", book->title);
38-
printf("Book Author:\n%s\n", book->author);
39-
printf("Book Price:\n%f\n", book->price);
40-
printf("Book Publish Date(2000-12-1):\n%hd-%hd-%hd\n", book->date.year,
41-
book->date.month, book->date.day);
42-
printf("Book Publisher:\n%s\n", book->publisher);
39+
void printBook(struct Book *book)
40+
{
41+
printf("Book Title:\n%s\n", book->title);
42+
printf("Book Author:\n%s\n", book->author);
43+
printf("Book Price:\n%f\n", book->price);
44+
printf("Book Publish Date:\n%hd-%hd-%hd\n", book->date.year,
45+
book->date.month, book->date.day);
46+
printf("Book Publisher:\n%s\n", book->publisher);
4347
}
44-
void InitLibrary(struct Book *Library[]) {
45-
for (int i = 0; i < MAX_SIZE; i++) {
46-
Library[i] = NULL;
47-
}
48+
void InitLibrary(struct Book *library[])
49+
{
50+
for (int i = 0; i < MAX_SIZE; i++)
51+
{
52+
library[i] = NULL;
53+
}
4854
}
49-
void printLibrary(struct Book *Library[]) {
50-
for (int i = 0; i < MAX_SIZE; i++) {
51-
if (Library[i] != NULL) {
52-
printBook(Library[i]);
53-
putchar('\n');
55+
void printLibrary(struct Book *library[])
56+
{
57+
for (int i = 0; i < MAX_SIZE; i++)
58+
{
59+
if (library[i] != NULL)
60+
{
61+
printBook(library[i]);
62+
putchar('\n');
63+
}
5464
}
55-
}
5665
}
57-
void releaseLibrary(struct Book *Library[]) {
58-
for (int i = 0; i < MAX_SIZE; i++) {
59-
if (Library[i] != NULL) {
60-
free(Library[i]);
66+
void releaseLibrary(struct Book *library[])
67+
{
68+
for (int i = 0; i < MAX_SIZE; i++)
69+
{
70+
if (library[i] != NULL)
71+
{
72+
free(library[i]);
73+
}
6174
}
62-
}
6375
}
64-
int main() {
65-
struct Book *library[MAX_SIZE];
66-
struct Book *ptr = NULL;
67-
char ch;
68-
int index = 0;
69-
InitLibrary(library);
70-
while (1) {
71-
printf("Do you need to input information(Y/N):\n");
72-
do {
73-
ch = getchar();
74-
// printf("You input %c\n",ch);
75-
} while (ch != 'Y' && ch != 'y' && ch != 'N' && ch != 'n');
76+
int main()
77+
{
78+
struct Book *library[MAX_SIZE];
79+
struct Book *ptr = NULL;
80+
char ch;
81+
int index = 0;
82+
InitLibrary(library);
83+
while (1)
84+
{
85+
printf("Do you need to input information(Y/N):\n");
86+
do
87+
{
88+
ch = getchar();
89+
// printf("You input %c\n",ch);
90+
} while (ch != 'Y' && ch != 'y' && ch != 'N' && ch != 'n');
7691

77-
if (ch == 'Y' || ch == 'y') {
78-
if (index < MAX_SIZE) {
79-
ptr = (struct Book *)malloc(sizeof(struct Book));
80-
getInput(ptr);
81-
library[index] = ptr;
82-
index++;
83-
putchar('\n');
84-
} else {
85-
printf("Library is full,exiting...\n");
86-
break;
87-
}
88-
} else {
89-
break;
92+
if (ch == 'Y' || ch == 'y')
93+
{
94+
if (index < MAX_SIZE)
95+
{
96+
ptr = (struct Book *)malloc(sizeof(struct Book));
97+
getInput(ptr);
98+
library[index] = ptr;
99+
index++;
100+
putchar('\n');
101+
}
102+
else
103+
{
104+
printf("Library is full,exiting...\n");
105+
break;
106+
}
107+
}
108+
else
109+
{
110+
break;
111+
}
90112
}
91-
}
92-
printf("Printing Library......\n");
93-
printLibrary(library);
94-
releaseLibrary(library);
113+
printf("Printing Library......\n");
114+
printLibrary(library);
115+
releaseLibrary(library);
95116
}

0 commit comments

Comments
 (0)