Global pointer (head of linked list) not updated properly - c

I have searched a lot, before I ask this, but I can't get this small piece of code to work.
I know that using a global pointer (or variable) is considered a bad practice (instead of passing by reference) but I am forced to use this practice sadly.
What I am trying to do is to make a linked list which consists of nodes (struct with some info), and after every insert() the list is dynamically expanded by one node (unless the element in question already exists, in that case the member name is overwritten).
The pointer next points to the next element in the list (that's where I assign the new node from malloc().
The program compiles correctly and executes with the following output:
retrieve returned: (NULL) at every printf() call
That's why I believe the global pointer (head of the list) is not updated properly
I am sorry for this naive question but I can't seem to find where the assignment/allocation goes wrong, Anyway thanks in advance for your assistance.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
struct node{
char *id;
char *name;
struct node *next;
};
struct node* list; //list head
struct node* p; //pointer to list head
char *retrieve(char *id){
if(list == NULL)
return NULL; //list is empty, no element to return.
for(p = list; p != NULL; p = p->next)
if(strcmp(id, p->id) == 0)
return p->name;
return NULL;
}
void insert(char *id, char *name){
int exists = 0;
struct node* temp = NULL;
for(p = list; p != NULL; p = p->next){
if(strcmp(id, p->id) == 0){ //id already exists, overwrite with the new name.
free(p->name);
p->name = strdup(name);
exists = 1;
break;
}
}
if(exists) return;
//insert at the end of the list
temp = malloc(1 * sizeof(struct node));
if(temp == NULL){
printf("memory allocation failed\n");
return;
}
temp->id = strdup(id);
temp->name = strdup(name);
temp->next = NULL;
p = temp;
return;
}
int main(){
struct node* temp = NULL;
p = NULL;
list = NULL;
insert("145a","Jim");
insert("246b","Alice");
insert("322c","Mike");
printf("retrieve returned: %s\n\n", retrieve("145a"));
printf("retrieve returned: %s\n\n", retrieve("246b"));
printf("retrieve returned: %s\n\n", retrieve("322c"));
p = list;
while(p != NULL){ // node deletion starting from first to last element.
free(p->id);
free(p->name);
temp = p;
p = p->next;
free(temp);
}
return 0;
}

void insert(char *id, char *name)
{
struct node *temp = NULL, **pp;
/* Pointer to pointer points to the global */
for(pp = &list; *pp ; pp = &(*pp)->next){
if(strcmp(id, (*pp)->id) ) continue;
free((*pp)->name);
(*pp)->name = strdup(name);
return;
}
//insert at the end of the list
temp = malloc(sizeof *temp);
if(!temp ){
printf("memory allocation failed\n");
return;
}
temp->id = strdup(id);
temp->name = strdup(name);
temp->next = NULL;
*pp = temp;
return;
}
And you can even do without the *temp pointer:
void insert(char *id, char *name)
{
struct node **pp;
for(pp = &list; *pp ; pp = &(*pp)->next){
if(strcmp(id, (*pp)->id) ) continue;
free(p->name);
p->name = strdup(name);
return;
}
// pp now points to the terminal NULL pointer
*pp = malloc(sizeof **pp);
if(!*pp ){
printf("memory allocation failed\n");
return;
}
(*pp)->id = strdup(id);
(*pp)->name = strdup(name);
(*pp)->next = NULL;
return;
}

You never initialize list other than with NULL. In consequence,
char *retrieve(char *id){
if(list == NULL)
return NULL;
always returns NULL.

Related

what is wrong with this flushing function for hash table?

I have a weird problem with flushing the entire hash-table.
Data structures are as below:
typedef struct data_entry_{
char data[32];
struct data_entry_ *next;
}data_entry_t;
typedef struct table_entry_{
char hash[32];
struct data_entry_ *next_data;
struct table_entry_ *next_hash;
}table_entry_t;
typedef struct table_{
table_entry_t *next;
}table_t;
In main function, I initialize the table with below function
table_t *init(){
table_t *table = calloc(1, sizeof(table_t));
table_entry_t *node = calloc(1, sizeof(table_entry_t));
node->next_hash = NULL;
node->next_data = NULL;
strcpy(node->hash, "NULL");
table->next = node;
return table;
}
Add data to the table with below function:
int add(table_t *table, char *data){
table_entry_t *head = table->next;
table_entry_t *prev;
char hash[32];
hash_function(data, hash);
if(!strcmp(head->hash, "NULL")){
data_entry_t *item = calloc(1, sizeof(data_entry_t));
strcpy(item->data, data);
item->next = NULL;
strcpy(head->hash, hash);
head->next_data = item;
head->next_hash = NULL;
return 0;
}
while(head){
if(!strcmp(head->hash, hash)){
data_entry_t *temp = head->next_data;
data_entry_t *previous;
while(temp){
if(!strcmp(temp->data, data)){
printf("data exists\n");
return 0;
}
previous = temp;
temp = temp->next;
}
data_entry_t *item = calloc(1, sizeof(data_entry_t));
strcpy(item->data, data);
item->next = NULL;
previous->next = item;
return 0;
}
prev = head;
head = head->next_hash;
}
table_entry_t *pack = calloc(1, sizeof(table_entry_t));
data_entry_t *item = calloc(1, sizeof(data_entry_t));
strcpy(pack->hash, hash);
strcpy(item->data, data);
item->next = NULL;
pack->next_data = item;
prev->next_hash = pack;
return 0;
}
And the problem is with this function:
int flush(table_t *table){
table_entry_t *head = table->next;
table_entry_t *temp;
data_entry_t *current, *previous;
if(head->next_data == NULL){
printf("table is empty\n");
return -1;
}
strcpy(head->hash, "NULL");
while(head){
current = head->next_data;
while(current){
previous = current;
current = current->next;
free(previous);
}
temp = head;
head = head->next_hash;
free(temp);
}
return 0;
}
after calling flush, when I wanna show the table, I expect to see "table is empty" but apparently this function does not free any nodes. I really appreciate if anyone help me.
You expect that a pointer which got freed gets the value NULL.
That however is impossible, because free() only receives the value inside the pointer, not the address, i.e. not pointer to the pointer.
Your code assumes that a freed pointer is NULL here:
if(head->next_data == NULL){
That however will only be the case if you write the NULL value to the pointer.
There is some code which seems similar, strcpy(head->hash, "NULL");, but I trust that you do not expect that to write the non-string value NULL to the pointer.
You can make sure that all pointer which you free afterwards contain NULL, by writing it yourself. Near this free(previous);. It is however necessary to write NULl to the actual pointer in the linked list - and not e.g. to the variable previous. You can use the copy of the pointer stored in that variable to free, but not for overwriting the original.

Why am I getting a segmentation fault on this linked list creation?

I am very new to C and I am not understanding where the segmentation fault is occurring in my code. I am just trying to create a linked list which will be the type unit for a hash table. I understand that I probably didn't use a malloc where I was supposed to. The main function will run but as soon as I add one item I get the segmentation fault.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct word_link
{
char* val;
struct word_link * next;
} word_link;
void add_to_list(char* word, word_link *head);
void add_to_list(char* word, word_link *head){
int i;
word_link * temp = NULL;
word_link * p = NULL;
temp = (word_link*)malloc(sizeof(word_link));
temp->val = word;
temp->next = NULL;
if(head == NULL){
head = temp;
} else {
p = head;
while(p->next != NULL){
p = p->next;
}
p->next = temp;
}
}
void main() {
int i = 0;
struct word_link *lst = malloc(sizeof(word_link));
char* word = "";
while(i == 0){
printf("what to add? ");
scanf("%s",word);
add_to_list(word, lst);
printf("continue? ");
scanf("%d", i);
}
printf("%s", lst->val);
printf("%s", "asdf;kl");
}
Your head is local to add_to_list function and will be destroyed once control exits add_to_list function.
Also any changes done to head inside add_to_list will not affect the original head lst.
Solution:
You can pass the reference of original head to insert in order to retain changes made in add_to_list as below.
void add_to_list(char* word, word_link **head);
void add_to_list(char* word, word_link **head){
int i;
word_link * temp = NULL;
word_link * p = NULL;
temp = (word_link*)malloc(sizeof(word_link));
if (temp == NULL) return;
temp->val = word;
temp->next = NULL;
if(*head == NULL){
*head = temp;
} else {
p = *head;
while(p->next != NULL){
p = p->next;
}
p->next = temp;
}
}
And you call add_to_list as below.
add_to_list(word, &lst);
Another Issue:
char* word = ""; this will create word as pointer to immutable string literal. Modifying the word content scanf("%s",word); will result in UB and since every time you are passing the same pointer to add_to_list every node in the list will point to same word.
You might want to declare it as below.
char *word = malloc(256);
Inside the while loop and pass it to add_to_list.

C - Linked Lists - Deleting Head - Segmentation Fault

I am working on a problem for a class and we're learning linked lists in C. I was given a section of code to complete, specifically the delete a node section and I'm having a problem deleting head. Every time I try to delete head I receive a segmentation fault. Can someone tell me what I'm doing wrong?
EDIT2
My teacher wrote everything but the lookup and delete functions.
I have fixed the glaring errors pointed out by the gentleman from Moscow and Mr. Petriuc, however the code still doesn't run. It does compile, but there is still a problem in head.
Here is the full code:
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include "linkedList.h"
// keep an unsorted array of char *'s, strings.
/*
Create an empty node, return 0 if fail, 1 if succeed
*/
struct node * createNode() {
struct node *p = (struct node *) malloc(sizeof(struct node));
if (p == NULL) return 0;
p->prev = p->next = NULL;
p->data = NULL;
}
/*
Lookup string in the list, return pointer to node of first occurence, NULL if not found.
*/
struct node * lookup(struct node *head, char *s) {
struct node *p;
for(p=head; p!=NULL; p=p->next){
if(strcmp(s,p->data)==0){
return p;
}
// just like print, but check if strcmp(s, p->data) == 0, and if so then return p
}
return NULL;
}
/*
Insert new string into the linked list, return 1 if success, 0 if fail.
*/
int insert(struct node **head, char *newS, int insertDuplicate) {
struct node *p = lookup(*head, newS);
if (p == NULL || insertDuplicate) {
// create a new node, put it at the front.
p = createNode();
if (p == NULL) return 0;
// put the string in the new node
p->data = (char *) malloc(sizeof(char) * (1 + strlen(newS)));
if (p->data == NULL) return 0;
strcpy(p->data, newS);
// note: make changes and use old head before setting the new head...
p->next = *head; // next of new head is previous head
if (*head != NULL)
(*head)->prev = p; // previous of old head is new head
*head = p; // set the new head
}
return 1;
}
/*
Remove string from list if found, return 1 if found and deleted, 0 o/w.
*/
int delete(struct node **head, char *s) {
struct node *p,*pr,*ne;
// first do a lookup for string s, call lookup.
p=lookup(*head, s);
if(p==*head){
*head = p->next;
free(p);
return 1;
}
if(p!=NULL){
printf("%s",p);
pr = p->prev;
ne = p->next;
free(p->data);
free(p);
if(pr==NULL||ne==NULL){
return 0;
}
pr->next=ne;
ne->prev=pr;
// if lookup returns NULL, done, return 0.
// if lookup returns p, not NULL,
// pr = p->prev, ne = p->next
// set pr->next to ne, ne->prev to pr
// but what if pr or ne is NULL
// and note that we need node **head because if delete head,
// need to update head pointer back in calling function, in
// here if you want head probably do *head. like in insert.
// also, before the pointer to the one you're deleting is gone,
// free p->data and p.
return 1;
}
return 0;
}
void print(struct node *head) {
struct node *p;
for(p = head; p != NULL ; p = p->next) {
printf("%s\n", p->data);
}
}
You are doing
p->next = *head;
But p is not assigned anywhere.
Your function does not make sense. You call the function lookup three times.
Moreover you use pointers that were not initialized like for example
p->next = *head;
or
printf("%s",p);
pr = p->prev;
ne = p->next;
The function can be written the following way
int delete( struct node **head, char *s )
{
int success;
struct node *target = lookup( *head, s );
if ( ( success = target != NULL ) )
{
if ( target->prev != NULL )
{
target->prev->next = target->next;
}
else
{
*head = target->next;
}
if ( target->next != NULL )
{
target->next->prev = target->prev );
}
free( target );
}
return success;
}
Take into account that the second parameter of the function and the corresponding parameter of the function lookup should be declared with qualifier const
int delete( struct node **head, const char *s ) ;
^^^^^
struct node * lookup( struct node *head, const char *s );
^^^^^^
Simplified delete() function. I inlined lookup() because the function as it is is worthless (you need a pointer to pointer, not a pointer to act upon)
/*
Remove string from list if found, return 1 if found and deleted, 0 o/w.
*/
int delete(struct node **head, char *s) {
struct node *tmp;
// first do a lookup for string s, no need to call call lookup.
for( ;*head; head = &(*head)->next ){
if (!strcmp( (*head)->data, s)) break;
}
if (!*head) return 0; // not found
tmp = *head
*head = tmp->next
free(tmp);
return 1;
}

linked list insertion at end when header node is not null

I'm having a problem with inserting a node at the end of a linked list. It's not being executed when the start node is not null and I don't understand the problem. Please help me out here. The function is called second time but is not going to the else block.
typedef struct token_Info
{
int linenumber;
char* token;
char value[200];
struct token_Info* next;
} token_Info;
token_Info *tokenlist;
token_Info* insert_at_end( token_Info *list,char *name)
{
printf("token identified \t");
token_Info *new_node;
token_Info *temp,*start;
start = list ;
char *tempname ;
tempname = name;
new_node= malloc(sizeof(token_Info));
new_node->token = malloc(sizeof(strlen(tempname)+1));
strcpy(new_node->token,tempname);
new_node->next= NULL;
// printf("%d",strlen(tempname));
if(new_node == NULL){
printf("nFailed to Allocate Memory");
}
if(start==NULL)
{
start=new_node;
return start;
}
else
{
printf("anvesh");
temp = start;
while(temp->next != NULL)
{
temp = temp ->next;
}
temp->next = new_node;
return temp;
}
}
tokenlist = insert_at_end(tokenlist,"TK_BEGIN");
tokenlist = insert_at_end(tokenlist,"TK_BEGIN1");
UPDATE
I found two bugs, the first was the head of the list was not being returned when appending the list. The other in the memory allocation for the token string which incorrectly used sizeof.
I repositioned the test of the malloc() return value, and added a second one. I removed several unnecessary temporary variables that were cluttering the code. I added two functions, show_list() and free_list(). Finally, remember that the value string field is still uninitialised.
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef struct token_Info
{
int linenumber;
char* token;
char value[200];
struct token_Info* next;
} token_Info;
token_Info* insert_at_end( token_Info *list, char *name)
{
token_Info *new_node, *temp;
new_node= malloc(sizeof(token_Info));
if(new_node == NULL){ // repositioned
printf("\nFailed to allocate node memory\n");
exit(1); // added
}
new_node->token = malloc(strlen(name)+1); // removed sizeof
if(new_node->token == NULL){ // added
printf("\nFailed to allocate token memory\n");
exit(1);
}
strcpy(new_node->token, name);
new_node->next= NULL;
if(list==NULL)
return new_node;
// append
temp = list;
while(temp->next != NULL)
temp = temp->next;
temp->next = new_node;
return list; // original head
}
void free_list( token_Info *list)
{
token_Info *temp;
while (list) {
temp = list->next;
free(list->token);
free(list);
list = temp;
}
}
void show_list( token_Info *list)
{
printf ("\nCurrent list:\n");
while (list) {
printf ("%s\n", list->token);
list = list->next;
}
}
int main(int argc, char **argv)
{
token_Info *tokenlist = NULL;
tokenlist = insert_at_end(tokenlist, "TK_BEGIN");
show_list(tokenlist);
tokenlist = insert_at_end(tokenlist, "TK_SECOND");
show_list(tokenlist);
tokenlist = insert_at_end(tokenlist, "TK_FINAL");
show_list(tokenlist);
free_list(tokenlist);
return 0;
}
Program output:
Current list:
TK_BEGIN
Current list:
TK_BEGIN
TK_SECOND
Current list:
TK_BEGIN
TK_SECOND
TK_FINAL
The question could also be whether you want tokenlist to be a running end of the list, or remain at the start.
As of right now, your first call:
tokenlist = insert_at_end(tokenlist,"TK_BEGIN");
has tokenlist being the only node in the list.
The second call tokenlist = insert_at_end(tokenlist,"TK_BEGIN1"); returns 'temp' which happens to also be the 'TK_BEGIN' node, ( ie, the first node )
If you want the return value to be the last element, you would return new_node instead of temp. If you want to retain the start, you would return start;
All that said:
The calls to it are not part of any function,
I just ran it with the calls in main and got this output:
int main(void){
tokenlist = insert_at_end(tokenlist,"TK_BEGIN");
tokenlist = insert_at_end(tokenlist,"TK_BEGIN1");
return 0;
}
$> ./a.out
token identified token identified anvesh

singly linked list error while adding a node

My problem is that, when i try to add a node to my singly linked list for the first time, everything goes expected, but when i want to add another node, my program crashes. Seems like my error is caused by trying to write to 0 address. however i can't seem to find the error in my code. am i using malloc right ?
Here is the code :
typedef struct linkedList
{
int StudentId;
char name[100];
char dep[100];
struct linkedList *next;
} LinkedList;
LinkedList *head = NULL;
LinkedList *current = NULL;
LinkedList *createList(int val, char name[], char dep[])
{
LinkedList *ptr = (LinkedList *)malloc(sizeof(LinkedList));
if (ptr == NULL)
{
printf("Node Creation Failed\n");
return NULL;
}
ptr ->StudentId = val;
strcpy(ptr -> name, name);
strcpy(ptr ->dep, dep);
ptr ->next = NULL;
head = current = ptr;
return ptr;
}
LinkedList *addToList (int val, char name[], char dep[])
{
if (head == NULL)
{
return (createList(val, name, dep));
}
else
{
LinkedList *ptr = (LinkedList *)malloc(sizeof(LinkedList));
if (ptr = NULL)
{
printf("Node Creation Failed\n");
return NULL;
}
ptr -> StudentId = val;
strcpy(ptr ->name, name);
strcpy(ptr ->dep, dep);
ptr -> next = NULL;
current -> next = ptr;
current = ptr;
return ptr;
}
}
in main function:
AddtoList(10,"abc","abc");
calls createList, no problem but
If i use AddtoList again, program crashes, createList and AddtoList are really similar to each other, can't figure out what the problem is.
Change
if (ptr = NULL)
to
if (ptr == NULL)
In your case, ptr is assigned to NULL and expression within if is evaluated to 0. Control goes to ptr -> StudentId = val;. It tries to access write protected memory, hence the crash.

Resources