Segfault and logic errors - c

I am making a linked list program which utilizes several functions to simplify adding or removing nodes from the list. I believe that my logic is alright with allocating and adding a new node but I am still getting a seg fault. Could you look over my logic and explain what is causing the seg fault? I am very inexperienced with linked lists. Thank you!
This is my function declarations
#define SIMPLELL_H
#include<stdio.h>
#include<stdlib.h>
typedef struct Node
{
int data;
struct Node *next;
}node_t;
node_t* head;
void printList();
void append(int num);
void addFront(int num);
void deleteList();
void removeNode(int num);
int length();
#endif
This is the function definitions
void printList(){
// declare temp to traverse the LL and print each value
node_t *temporary = head;
while(temporary != NULL){
printf("%d, ", temporary->data);
temporary = temporary->next;
}
if (head == NULL){
printf("This list is empty.\n");
}
}
void append(int num){
//declare temporary pointer to traverse LL
node_t *tmp = head;
node_t *prev = NULL;
//malloc the new node
node_t *new = malloc(sizeof(node_t));
if(head == NULL){
//initialize the new node
new->data = num;
new->next = NULL;
head->next = new;
}
else{
//initialize the new node
new->data = num;
new->next = NULL;
//loop to traverse to the last value in the node
while(tmp != NULL){
prev = tmp;
tmp = tmp->next;
}
prev->next = new;
}
}
void addFront(int num){
//allocate memory for new node and allocate local variable equal to head
node_t *new = malloc(sizeof(node_t));
new->data = num;
new->next = head;
head = new;
}
void deleteList(){
node_t *tmp = head;
node_t *prev = NULL;
while(tmp != NULL){
prev = tmp;
tmp = tmp->next
free(prev);
}
}
void removeNode(int num){
//temp is used to traverse the ll until I find the node with the value
//then the previous value is linked to the next value removing the middle value.
node_t *temp = head;
node_t *prev = NULL;
if(head == NULL){
printf("List is not initialized\n");
return;
}
while(temp->data != num){
prev = temp;
temp = temp->next;
if (temp->data == num){
prev->next = temp->next;
free(prev);
}
else{
printf("Value is not in the list\n");
return;
}
}
int length(){
int length = 0;
node_t *temp = head;
while (temp != NULL){
length += 1;
temp = temp->next;
}
return length;
}
This is the driver function which I should not have to change
#include "SimpleLL.h"
#include "SimpleLL.c"
int main()
{
/*head is a global variable*/
head = NULL;
int size =0;
removeNode(1);
append(2);
append(3);
addFront(1);
append(4);
printList();
size = length();
printf("size now %d\n",size);
removeNode(8);
printList();
removeNode(1);
printList();
removeNode(4);
printList();
deleteList();
printList();
size = length();
printf("size after deletion %d\n",size);
return 0;
}

removeNode contains no test for reaching the end of the list. So if the value is not found in the list, it will dereference the NULL pointer at the end and crash.
This will be the case in the very first test, which attempts to remove a node when the list is empty, as well as for removeNode(8).
Your removeNode function also needs to free() the node it removed. Currently it is freeing the node that came after the one removed, which is still in the list and will cause undefined behavior when you access it later.

Related

Need clear understanding why value isn't affected even if passed by reference? [duplicate]

This question already has an answer here:
Dynamic memory access only works inside function
(1 answer)
Closed last year.
I have a question on which I seek clarity with pointers. I am coming from javascript so I am having a hard time with pointers. I have written singly linked list code mostly by myself and the code is fully working. I have created a function to delete the particular item in the linked list. The function in the main is this:
insertAtMiddle(&head, 3, 500);
insertAtMiddle(&head, 100, 500);
There is one thing I can't understand. First I would like to show the code of my delete function.
void insertAtMiddle(node_t **head, int location, int newData){
node_t *temp = *head;
while(temp->next != NULL){
if (temp->data == location)
{
break;
}
//Shouldn't that also change the original head or move the head to the left as it is passed by reference
temp=temp->next;
}
if (temp->data != location)
{
printf("No location found for replacement!");
}
//Create new node
node_t *newNode = malloc(sizeof(node_t));
newNode->data = newData;
newNode->next = temp->next;
temp->next = newNode;
}
My question is shouldn't the temp=temp->next; inside the while loop should also affect or modify the original head? Head has been passed as a reference to this function. My confusion arises because *temp = *head, temp has been assigned head.
My full code:
#include <stdio.h>
#include <stdlib.h>
typedef struct node {
int data;
struct node *next;
} node_t;
void viewAllNodes(node_t *head){
node_t *tmp = head;
printf("\n");
while (tmp != NULL)
{
printf("%d--->", tmp->data);
tmp=tmp->next;
}
printf("\n");
}
void insertAtBegining(node_t **head, int data){
node_t *tmp = malloc(sizeof(node_t));
tmp->data = data;
tmp->next = *head;
*head = tmp;
}
void deleteNode(node_t **head,int data){
node_t *tmp = *head, *nodeToDelete = NULL;
//see two nodes in advance
while(tmp->next != NULL){
if (tmp->next->data == data)
{
nodeToDelete = tmp->next;
break;
}
tmp = tmp->next;
}
if (nodeToDelete == NULL)
{
printf("No node found to delete");
return;
}
tmp->next = nodeToDelete->next;
free(nodeToDelete);
}
void insertAtEnd(node_t **head, int data){
node_t *newNode = malloc(sizeof(node_t));
newNode->data = data;
newNode->next = NULL;
node_t *tmp = *head;
while (tmp->next != NULL)
{
tmp = tmp->next;
}
tmp->next = newNode;
}
node_t *searchNode(node_t *head, int value){
node_t *tmp = head;
while (tmp->next != NULL)
{
if(tmp->data == value){
return tmp;
}
tmp=tmp->next;
}
return NULL;
}
void insertAtMiddle(node_t **head, int location, int newData){
node_t *temp = *head;
while(temp->next != NULL){
if (temp->data == location)
{
break;
}
//Shouldn't that also change the original head as it is passed by
temp=temp->next;
}
if (temp->data != location)
{
printf("No location found for replacement!");
}
//Create new node
node_t *newNode = malloc(sizeof(node_t));
newNode->data = newData;
newNode->next = temp->next;
temp->next = newNode;
}
int main(){
node_t *head = NULL;
insertAtBegining(&head, 1);
insertAtBegining(&head, 2);
insertAtBegining(&head, 3);
insertAtBegining(&head, 4);
insertAtBegining(&head, 5);
insertAtBegining(&head, 6);
insertAtEnd(&head, 8);
insertAtEnd(&head, 9);
insertAtBegining(&head, 100);
viewAllNodes(head);
deleteNode(&head, 1);
deleteNode(&head, 8);
insertAtMiddle(&head, 3, 500);
insertAtMiddle(&head, 100, 500);
viewAllNodes(head);
return 0;
}
You are basically doing this:
#include <stdio.h>
void foo(int* head) {
int temp = *head;
temp = 1234;
}
int main()
{
int bar = 0;
foo(&bar);
printf("bar = %d\n", bar);
}
You expect 1234 as output, but the actual output is 0.
temp is just a local copy, modifying temp will just modify temp and nothing else.

Element deletion in single linked list at head not working

#include <stdio.h>
#include <stdlib.h>
struct node {
int data;
struct node *next;
};
struct node *head = NULL;
struct node *second = NULL;
struct node *third = NULL;
void insertAtBeg(struct node *n, int data) {
struct node *temp;
temp = (struct node *)malloc(sizeof(struct node));
temp->data = data;
temp->next = head;
head = temp;
}
void insertAtEnd(struct node *n, int data) {
struct node *temp;
temp = (struct node*)malloc(sizeof(struct node));
temp->data = data;
temp->next = NULL;
while (n->next != NULL) {
n = n->next;
}
n->next = temp;
}
void deleteElement(struct node *head, int data) {
if (head->data == data) {
struct node *temp;
temp = head;
head = head->next;
free(temp);
printf("after deletion at head in function\n");
printList(head);
}
}
void printList(struct node *n) {
while (n != NULL) {
printf("%d\n", n->data);
n = n->next;
}
}
void main() {
head = (struct node*)malloc(sizeof(struct node));
second = (struct node*)malloc(sizeof(struct node));
third = (struct node*)malloc(sizeof(struct node));
head->data = 1;
head->next = second;
second->data = 2;
second->next = third;
third->data = 3;
third->next = NULL;
printList(head);
insertAtBeg(head, 0);
printf("after insertion at beginning\n");
printList(head);
insertAtEnd(head, 4);
printf("after insertion at End\n");
printList(head);
deleteElement(head, 0);
printf("after deletion at head in main\n");
printList(head);
}
output of the code is
1
2
3
after insertion at beginning
0
1
2
3
after insertion at End
0
1
2
3
4
after deletion at head in function
1
2
3
4
after deletion at head in main
0
1
2
3
4
Why is there a difference in output of the function called in main and the function called in another function.ie.after deletion at head in function and after deletion at head in main, when both are supposed to be deleting element from the same list
The problem is you need a way to modify the head of the list when inserting and/or deleting elements from the list.
A simple way to do this is for these functions to return a potentially updated value of the head pointer and for the caller to store this return value into it's head variable.
Here is a modified version of your code with these semantics:
#include <stdio.h>
#include <stdlib.h>
struct node {
int data;
struct node *next;
};
struct node *insertAtBeg(struct node *head, int data) {
struct node *temp;
temp = (struct node *)malloc(sizeof(struct node));
// should test for memory allocation failure
temp->data = data;
temp->next = head;
return temp;
}
struct node *insertAtEnd(struct node *head, int data) {
struct node *temp;
struct node *n;
temp = (struct node*)malloc(sizeof(struct node));
// should test for memory allocation failure
temp->data = data;
temp->next = NULL;
if (head == NULL)
return temp;
n = head;
while (n->next != NULL) {
n = n->next;
}
n->next = temp;
return head;
}
struct node *deleteElement(struct node *head, int data) {
// delete the first node with a given data
if (head->data == data) {
struct node *temp = head;
head = head->next;
free(temp);
} else {
struct node *n = head;
while (n->next != NULL) {
if (n->next->data == data) {
struct node *temp = n->next;
n->next = temp->next;
free(temp);
break;
}
}
}
return head;
}
void printList(const struct node *n) {
while (n != NULL) {
printf("%d\n", n->data);
n = n->next;
}
}
int main() {
struct node *head = NULL;
head = insertAtBeg(head, 1);
head = insertAtEnd(head, 2);
head = insertAtEnd(head, 3);
printList(head);
head = insertAtBeg(head, 0);
printf("after insertion at beginning\n");
printList(head);
head = insertAtEnd(head, 4);
printf("after insertion at End\n");
printList(head);
head = deleteElement(head, 0);
printf("after deletion at head in main\n");
printList(head);
// should free the list
return 0;
}
An alternative is to pass the address of the list head pointer so the function can modify it if needed.
Here is a modified version of your code with this alternative approach:
#include <stdio.h>
#include <stdlib.h>
struct node {
int data;
struct node *next;
};
struct node *insertAtBeg(struct node **headp, int data) {
struct node *temp = malloc(sizeof(*temp));
if (temp != NULL) {
temp->data = data;
temp->next = *headp;
*headp = temp;
}
return temp;
}
struct node *insertAtEnd(struct node **headp, int data) {
struct node *temp = malloc(sizeof(*temp));
if (temp != NULL) {
temp->data = data;
temp->next = NULL;
if (*headp == NULL) {
*headp = temp;
} else {
struct node *n = *headp;
while (n->next != NULL) {
n = n->next;
}
n->next = temp;
}
}
return temp;
}
int deleteElement(struct node **headp, int data) {
// delete the first node with a given data
struct node *head = *headp;
if (head->data == data) {
*headp = head->next;
free(temp);
return 1; // node was found and freed
} else {
struct node *n = head;
while (n->next != NULL) {
if (n->next->data == data) {
struct node *temp = n->next;
n->next = temp->next;
free(temp);
return 1; // node was found and freed
}
}
return 0; // node not found
}
}
void printList(const struct node *n) {
while (n != NULL) {
printf("%d\n", n->data);
n = n->next;
}
}
int main() {
struct node *head = NULL;
insertAtBeg(&head, 1);
insertAtEnd(&head, 2);
insertAtEnd(&head, 3);
printList(head);
insertAtBeg(&head, 0);
printf("after insertion at beginning\n");
printList(head);
insertAtEnd(&head, 4);
printf("after insertion at End\n");
printList(head);
deleteElement(&head, 0);
printf("after deletion at head in main\n");
printList(head);
// free the list
while (head != NULL) {
deleteElement(&head, head->data);
}
return 0;
}
This alternative approach uses double pointers, so it is a bit more difficult for beginners to comprehend, but it has a strong advantage: the functions can update the list pointer and provide a meaningful return value that can be tested to detect errors. For example insertAtBeg() and insertAtEnd() return NULL if the new node could not be allocated but preserve the list. Similarly deleteElement() can return an indicator showing whether the element was found or not.
With this approach, you can write functions to pop the first or last element of the list, or the one at a given index, or one with a given data, while updating the list pointer as needed.
In the function void deleteElement(struct node *head,int data) you are passing a pointer to the head node. If you make changes to the node, then that works because you are pointing to the actual node. However, the variable head is a local copy of the pointer, which is not the one in main. When you change head to head->next that is only changing the local copy, so it has no effect outside deleteElement.
ADVANCED LEVEL POINTERS
To actually change head you have to pass a pointer to it, making a double pointer:
void deleteElement(struct node **phead,int data) {
struct node *temp;
temp = *phead;
*phead = (*phead)->next;
this means you have to pass the address of head &head as the parameter.

In my Singly linked List implementation why is it even though I allocated memory for the node to be freed, the pointer to the Node isn't NULL?

Using the delete_SLL function I want to delete the head of this singly linked list(head = 4). Although I get the correct output, the var struct Node* "temp" holding the value of the head isn't NULL. What is it about the variable "temp" that the free function not like? Is the node temp not Malloc-ed when setting it equal to the list head?
Source:Deleting a Node
Code:
#include <stdio.h>
#include <stdlib.h>
struct Node{
int item;
struct Node* next;
};
struct List{
struct Node* head;
struct Node* tail;
};
int SLL_empty(struct List* lst){
return lst->head == NULL ;
}
//newLst work
struct List newLst(){
struct List lst;
lst.head = NULL;
lst.tail = NULL;
return lst;
}
//Inserts a node to the front of the list[WORKS]
void insert_SLL(struct List* lst, int x){
struct Node* nde = (struct Node*)malloc(sizeof(struct Node));
nde->next = lst->head;
nde->item = x;
if (SLL_empty(lst))
lst->tail=nde;
lst->head = nde;
}
//Deletes a given Node
void delete_SLL(struct List* lst, int x){
struct Node* temp = (struct Node*)malloc(sizeof(struct Node));;
temp = lst->head;
struct Node* prev = NULL;`enter code here`
//If the head has the key
if (temp != NULL && temp->item == x){
lst->head = temp->next;
temp->next = NULL;
free(temp);
}
// stops once the key is found
while(temp != NULL && temp->item != x){
prev = temp;
temp= temp->next;
}
//If not in list
if (temp == NULL) return;
//If middle
if (temp != NULL && temp->item == x){
prev->next = temp->next;
temp->next = NULL;
}
//if at the end
if (temp != NULL && temp->item == lst->tail->item){
lst->tail= prev;
prev->next = NULL;
}
free(temp);
}
int SLL_pop(struct List *list){
struct Node* nde = list->head;
int item = nde->item;
list->head = nde->next;
free(nde);
if (SLL_empty(list))
list->tail = NULL;
return item;
}
int main(int argc, const char * argv[]) {
int i;
struct List list = newLst();
for (i = 0; i < 5; ++i)
insert_SLL(&list, i);
// printf("The length of the linkedLst is: %d\n",SLL_length(&list));
delete_SLL(&list, 4);
while ( list.head != NULL )
printf("Node: %d\n", SLL_pop(&list));
return 0;
}
The main purpose of free() is to ask the OS take the allocated memory back to the system. You might not be able to "see" that but if you try to access any element at the "temp" afterward, you should get an error.
While the "temp" in the program is only a variable. C doesn't require to, and can't change the given pointer to NULL due to pass-by-value sense. It's the programmer's work to remember that this pointer is no longer valid.
Or you can set it to NULL manually each time you free a pointer.

Memory leak linked list

I'm having issue with an assignment for my CS class. I've been working with a friend, and we've identified that our code has a memory leak, but we can't find what the problem is. Essentially, the code is supposed to create a linked list with digits from 2-1000. Then, the code uses deletemultiples in order to delete numbers that aren't prime. It does this by taking a number and deleting any multiples of that number in the linked list. When we used valgrind, it returned a memory leak.
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
struct node{
int info;
struct node *next;
};
typedef struct node node;
node *inserthead(node *head, int a);
node *inserttail(node *head, int a);
node *deletemultiples(node *head, int a);
void printlist(node *head);
void freelist(node *head);
int main(){
node *head1 = NULL;
int i,j;
for(i = 2;i <= 1000;i++)
head1 = inserttail(head1,i);
for(i = 2; i <= 32; i++){
head1 = deletemultiples(head1,i);
}
printlist(head1);
freelist(head1);
}
node *inserthead(node *head, int a){
node *ptr;
ptr = (node*)malloc(sizeof(node));
ptr->info = a;
ptr->next = head;
return(ptr);
}
node *inserttail(node *head, int a){
node *ptr;
node *ptr2 = head;
ptr = (node*)malloc(sizeof(node));
ptr->info = a;
ptr->next = NULL;
if(head == NULL)
return(ptr);
else if (head->next == NULL){
head->next = ptr;
return(head);
}
while(head->next != NULL)
head = head->next;
head->next = ptr;
return(ptr2);
}
void printlist(node *head){
while(head!=NULL){
printf("%i ",head->info);
head = head->next;
}
printf("\n");
}
void freelist(node *head){
node *ptr = head;
while(head != NULL){
head = head->next;
free(ptr);
ptr = head;
}
}
node *deletemultiples(node *head, int a){
node *ptr = head, *temp = head;
while (ptr != NULL) {
if(ptr->info % a > 0){
ptr = ptr->next;
temp = temp->next;
}
else{
ptr = ptr->next;
temp->next = ptr;
}
}
return(head);
}
If anyone can help us figure out what we did wrong, it would be greatly appreciated!
Your deletemultiples() function never frees the nodes it unlinks, so freelist() never reaches them when it walks the list to delete it. Delete the nodes you remove the same way you did in freelist().
Alternatively, you could create an array of 1,000 nodes (It’s a good habit to turn constants like that into symbolic names.) and link and unlink nodes within that array as weak references. You would free the entire array in a single call when you destroy all lists that depend on it.
Kudos for looking for memory leaks in your code. It’ll save you a lot of grief.

inline implementation and function calling showing two different behavior in C?

I have been trying to implement the delete function for singly linked list.
My implementation is as follow.
typedef struct singly_linked_list{
int data;
struct singly_linked_list *next;
}sll;
Function to delete first node is as follow:
void delete_node_from_front(sll ** head){
sll ** temp;
*temp = *head;
(*head) = (*head)->next;
(*temp)->next = NULL;
free_node(temp);
}
Another function from where I am calling this function:
void delete_node_from_given_pos(sll ** head, int pos){
int i = 1;
sll * temp;
sll * prev;
temp = *head;
prev = *head;
if(pos == 1){
delete_node_from_front(head);
}
else{
while(temp != NULL && i < pos){
i++;
prev = temp;
temp = temp->next;
}
if(temp != NULL){
prev->next = temp->next;
temp->next = NULL;
free_node(&temp);
}
else{
printf("Node with given position is not present");
}
}
}
This implementation is giving me segmentation fault.
But If I replace the function call delete_node_from_front() in the imlementation of delete_node_from_given_pos() by the below code
(*head) = (*head)->next;
temp->next = NULL;
free_node(&temp);
program runs perfectly. The way I am calling the fucntion:
sll * head;
head = NULL;
int arr[_SIZE]=_ELEMENT;
create_list(&head, arr, _SIZE);
delete_node_from_given_pos(&head, 1);
print_list(head);
Implementation of print_list function
void print_list(sll * head){
int i = 0;
while(head != NULL){
printf("%d ", head->data);
head = head->next;
}
printf("\n");
}
implementation of free_node function:
void free_node(sll ** head){
free(*head);
}
What could be the possible reason for this behavior?

Resources