Freeing a struct - c

I have a linked list struct on me, and I used a node struct to help me store data and know where each node is pointing to. In the creation of my list, I did this:
struct list {
struct node *Head;
struct node *Tail;
}
struct node{
int val;
struct node * next;
}
struct list create_list(){
struct list new_list;
new_list = malloc(sizeof(struct list));
}
Then, when I am trying to free the list that I malloced for, I did this
void free_list(struct list linked_list){
free(linked_list);
}
However, I realize that I may have malloced wrong, because when you malloc, you need to assign it to a pointer. When I want to free the linked_list that I originally malloced for, nothing really changes.
I think this may be because the linked_list that I sent into free() is not a pointer, thus no changes have been made. I tried adding & and * to it, but I end up getting errors....
Any tips or help is appreciated.
Edit:
Fixed Typo on the struct list.
This is hw, so I cannot change the parameters of the functions, which is what is giving me a hard time freeing the linked list I made. This is because the function free_list() cannot change its parameter into a pointer linked list, so I don't know exactly what to do.
I will also take note of when I am mallocing for the linked list in the create_list function.

Yay for passing the linked list around by value rather than on the heap for something that small.
Is this more like what you want:
struct list create_list(){
struct list new_list = { NULL, NULL };
return list;
}
void free_list(struct list linked_list){
struct node *next;
for (struct node *node = linked_list.Head; node; node = next;)
{
temp = node->next;
free(node);
}
}
Thus freeing only the list nodes that were allocated with malloc().

Related

How do I delete a doubly linked list in C?

I've created a doubly linked list, filled it with values and now I want to delete it and remove all the values to avoid memory leaks. Here's what I wrote as well as the structs that were used when creating the doubly linked list. Both those functions will be called towards the end of the main function.
struct node
{
struct node *next;
struct node *prev;
char *value;
};
// The type for a list.
typedef struct list
{
struct node head;
} List;
// The type for a list position.
typedef struct list_pos
{
struct node *node;
} ListPos;
void list_destroy(List *lst)
{
List p,q;
p = *lst;
while (p)
{
q = p.head->next;
free(p);
p = q;
}
*lst = NULL;
}
// Remove the value at the position and return the position of the next element.
ListPos list_remove(ListPos pos)
{
}
You appear to have the right general idea: you walk the list and free each node, making sure to grab any needed data from each node (in particular, the pointer to the next node) while the node holding it still exists. Your case differs from some that you might have seen, however, because instead of handling the overall list via a bare pointer to the head node, you have a separate object, of a separate type (List / struct list), to represent the list itself. This approach has much to recommend it, including, especially, the use of (apparently) a dummy head node, which provides for a variety of algorithmic simplifications. This is usually how I write a linked list.
But because struct list is not struct node, you cannot set a list pointer equal to a node pointer. Instead, create a struct node * to track your position. The first node to free would be the one referenced by struct node *to_free = lst->head.next, and the one after that would be the one referenced by to_free->next.
Note that you might need to free the struct list, too.

Linked list node memory allocation

In creating a linked list we make a node structure and it consists of both data and a pointer to the next node. Later when we make a function to append elements onto the linked list, we make a temporary node to store the inputted data.
Let’s consider the following program-
#include<stdio.h>
struct node
{
int data;
struct node* link;
}
struct node* root=NULL;
void main(append)
{
struct node* temp;
temp= (struct node*)malloc(sizeof(struct node))
.....
}
My first question set:
In line 11, why do we need to mention (struct node*) before the malloc function?
What is the significance of that?
My second question set:
If we are making a doubly linked list which would have a node structure consisting of 2 pointers (for next and previous node), would we also initialize a pointer (for traversing the list) of the struct node type?
Is there a different way to initialize the pointer in that case?
The significance is to make bugs in your program,
The malloc will return void* and when you assign to your struct somthing* it will convert automaticlly.
You simply don't cast the result of malloc as it returns void* . There is one fine explanation here
A better solution might be :
struct node *temp;
temp = malloc(sizeof *temp);
why do we need to mention '(struct node*)' before the malloc function,
what is the significance of that?
By writing (struct node*) before the malloc function, you are type-casting the return value to the specified type. The cast here is optional and often frowned upon.
if we are making a doubly linked list which would have a node
structure consisting of 2 pointers(...
When making a doubly linked list, you should declare something like:
struct node {
int data;
struct node *next;
struct node *previous;
};
You can allocate space for a node by using the malloc function. The next and previous pointers are again pointers to struct nodes. Call malloc again to allocate space for the next element. For the first node, the previous should be NULL and for the last node, next should be NULL. Here is one implementation.
This is the because the return type of malloc is void*. (struct node*) is a cast using which you tell the compiler that you want to treat the value returned by malloc as a pointer to struct node.
For double linked list you can use,
struct node
{
int data;
struct node *next,*prev;
};
int main()
{
struct node *new_node=(struct node *)malloc(sizeof(node));
}
malloc returns the void pointer(can be checked and verified at the header ), and so the type casting is necessary while assigning it to the other type of variable.
Request you to read at the link https://www.tutorialspoint.com/cprogramming/c_type_casting.htm

Nodes as a pointer

Why the nodes in a linked list are declared as pointers? Nodes contains the pointer part in it to link to another node. Then why the nodes are itself a pointer?
struct node
{
int data;
struct node *link;
} *start;
Now we introduce nodes as
struct node *tmp;
Now this is a node which is a pointer to data type struct node..but for linking we use the link pointer to link the other node
Why dindnt we coded node as
struct node tmp;
only...is this because of allocating dynamic memory..or something more?
Yes, this is because the nodes are allocated dynamically.
struct node tmp could be using tmp as a dummy or sentinel node, where it's data is not used, only it's next pointer to the actual first node of a list.
struct node *tmp would be using tmp as a pointer to the first node of a list or as a working pointer to some node in a list when scanning a list.
In some cases a list structure is also used:
struct list{
struct node *head // pointer to first node
struct node *tail // optional: pointer to last node
size_t size; // optional: number of items in a list
}
A circular list could be implemented using just a tail pointer to the last node, which in turn would have a next pointer to the head/first node.

confused over the usage of free () in C

This is done by someone who is better in C programming than me.
confused over the usage of free () in C
below is the struct of linked list
typedef struct node {
int value;
struct node *next;
} ListNode;
typedef struct list {
ListNode *head;
} LinkedList;
after created a list with some nodes
his code does this when exit
void deleteList(LinkedList *ll) {
if (ll != NULL) {
ListNode *temp;
while (ll->head) {
temp = ll->head;
ll->head = temp->next;
free(temp);
}
free(ll);
}
}
The above is what I don't understand. Why he needs to create such complexity, why not just do free(ll).
Please help
thanks in advance.
Linked list is made up of individual objects that happen to point at each other. If you want to delete a list you have to delete all of its nodes. free() won't do that. It doesn't know that these objects make up a list. It doesn't even know that these objects contain pointers to anything. Therefore you need to iterate over the list and free each node by hand.
if you have a linked list, suposse that every "*" is a node.
0 1 2 3 4
head--> *--*--*--*--*
the first *, es the head, is you just do "free ll"
this will be on the memory
0 1 2 3 4
head-->nul *--*--*--*
the problem here, is, all the "memory" that you ask for those nodes will still be there, and now you can't know where is it (you have nothing poiting to that memory) for every malloc you need a free (not 100% true, but for simple things work).
What that algorithm do is:
get the reference to the next node (if you don't do this and you free the node, you won't be able to get the "next" node, becouse head will be pointing to nothing ).
free the head.
make head point to the reference that you get before.
You can do with the following struct alone
typedef struct node {
int value;
struct node *next;
} ListNode;
But every time you have to declare a global variable struct node *HEAD. In a bigger program it may confuse you. The author has done this so that you can create a linked list like a variable. Every time you have to create a new linked list all you have to do is declare
LinkedList *ll;
When there are two struct, one has to free the objects of both the struct.
That's because each pointer points to a memory location. You need to free all memory locations that were previously allocated. free(ll) would only remove the node pointed to by the ll pointer.

Preserving Structs without Dynamic Allocation

I've been battling with this problem for a few hours.
I have a doubly linked list implementation that does not require dynamic memory allocation.
Here's the insertion method. It inserts elem right before before. The struct list has two members, struct list_elem *next and struct list_elem *prev. list_elem is a struct that holds some data. Please note that this list implementation is correct. The problem is with the way I am using it. Please read on.
void list_insert(struct list_elem *before, struct list_elem *elem) {
/* "before" has to be an interior node or tail node to be able to insert "before" it */
assert (is_interior(before) || is_tail(before));
assert (elem != NULL);
elem->prev = before->prev;
elem->next = before;
before->prev->next = elem;
before-> = elem;
}
The way this list is initialized is as follows:
void list_init(struct list *list) {
assert(list != NULL);
list->head.prev = NULL;
list->head.next = &list->tail;
list->tail.prev = &list->head;
list->tail.next = NULL;
}
My main class does the following:
struct list some_list;
static struct list_elem head;
some_list.head = head;
static struct list_elem tail;
some_list.tail = tail.
list_init(&some_list);
This basically creates two static list_elem structs, head and tail. And passes it into the initialization function which will wire them up together.
Now to create an element, I do the following - here's what I am doing wrong
struct list_elem element_struct;
/* Initialize element_struct members here */
struct list_elem *data = &element_struct;
list_insert( list_begin(some_list), data);
Now this works to insert one item. Since some_list.head will point to data which will point to some_list.tail. My problem is that I do this in a loop. So, as any list does, it will enter multiple data in there. I either get a segfault or some other error saying my element is not an interior or tail node (due to assert) since in each loop iteration, the data struct, element_struct will be reinitialized. (list_elem holds pointers to each other).
So my question is, how would I preserve the nodes that have been inserted? The list is not supposed to use any dynamic allocation itself. In that case, would I dynamically allocate my list_elem structs, and pass a dynamically allocated element into the list?
If so, how can I dynamically allocate a struct?
If you need to dynamically-allocate a struct instance, it's as simple as MyStruct *p = malloc(sizeof(*p));, and then an associated free(p); at some point.

Resources