Я довольно новичок в программировании на C.
У меня есть задание, в котором мы должны создать двусвязный список целых чисел и написать несколько функций для управления ими. Нас просят предотвратить утечки памяти, но я не совсем уверен, как это сделать.
Мне нужно malloc
несколько раз, чтобы создавать и хранить узлы при создании связанного списка, и я почти уверен, что не стоит malloc
достаточно места для узла, а затем освободить указатель на него там же.
Поэтому я предпочитаю освободить все узлы в основной функции, когда я выведу их содержимое на экран, и они больше не нужны. Я попытался реализовать функцию kill
, которая принимает в качестве входных данных ссылку head
на первый узел в списке и выполняет итерацию по узлам, освобождая их по мере их поступления.
Я зашел так далеко, что установил valgrind, чтобы попытаться выяснить, не было ли утечек памяти, и похоже, что они еще есть. Я понятия не имею, откуда они берутся и как решить проблему.
Вот весь код:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
typedef struct Node{
int data;
struct Node *next;
struct Node *previous;
}Node;
void print_dll(Node *head){
Node *curr = head;
while(curr != NULL){
printf("%d\t", curr->data);
curr = curr->next;
}
puts(" ");
}
Node* create_dll_from_array(int array [], int arrSize){
//this is a function that creates a doubly linked list
//with the contents of the array
Node* current = (Node *) malloc (sizeof(Node * ));
current->data = array[arrSize-1];
current -> next = NULL;
for(int i = 2; i <= arrSize; i++){
//create a new node
Node * temp = (Node*)malloc(sizeof(Node*));
//I would like the dll to be in the same order as the array, I guess it isn't strictly necessary
temp ->data = array[arrSize-i];
temp -> next = current;
current-> previous = temp;
//now make temp the current
current = temp;
}
current-> previous = NULL;
return current;
}
void insert_after(Node* head, int valueToInsertAfter, int valueToInsert ){
if(head != NULL){
Node * current = head;
while(current-> data != valueToInsertAfter){
//this while loop brings 'current' to the end of the list if
//the searched value is not there
if(current-> next != NULL){
current = current->next;
}else{
break;
}
}
//after exiting this loop, the current pointer is pointing
//either to the last element of the dll or to the element
//we need to insert after
Node *new = (Node *) malloc (sizeof(Node *));
new->data = valueToInsert;
new->next = current->next;
new->previous = current;
if(current->next != NULL){
(current->next)->previous = new;
}
current->next = new;
}
}
void delete_element(Node* head, int valueToBeDeleted){
//work in progress
}
void kill(Node *head){
//this is my attempt at freeing all the nodes in the doubly linked list
Node *current;
while(head!=NULL){
current = head;
head = head->next;
free(head);
}
}
int main(){
int array [5] = {11, 2, 7, 22, 4};
Node *head;
/*Question 1*/
//creates a doubly linked list from the array below
head = create_dll_from_array(array, 5); ///size of the array is 5
/* Question 2 */
// print_dll(head);
/*Question 3*/
// to insert 13 after the first appearance of 7
insert_after(head, 7, 13);
print_dll(head);
//to insert 29 after first appearance of 21
insert_after(head, 21, 29);
print_dll(head);
/*Question 6*/
//create a function to free the whole list
kill(head);
return 0;
}
Основная функция здесь дается нам профом, мы должны построить функцию вокруг него.
Я не знаю, почему это все еще ведет к утечкам памяти, и если я, честно говоря, я не знаю, где еще они могут произойти. Насколько я знаю, мне нужно хранить всю память почти до последней минуты.
Пожалуйста, помогите, я здесь довольно потерян.
Спасибо!