I'm having these two structures:
typedef struct node {
int info;
struct node *left, *right;
}NODE;
typedef struct bst {
NODE *root;
}BST;
And these functions:
NODE *newNode(int info) {
NODE *tmp = (NODE *)malloc(sizeof(NODE));
tmp->left = tmp->right = NULL;
tmp->info = info;
return tmp;
}
void addTree(BST **bst, int info) {
if (*bst == NULL) {
(*bst)->root = newNode(info); // <- Breaks the program
return;
}
else while ((*bst)->root != NULL) {
if (info < (*bst)->root->info)
(*bst)->root = (*bst)->root->left;
if (info >(*bst)->root->info)
(*bst)->root = (*bst)->root->right;
}
(*bst)->root->info = info; // <- Breaks the program
}
I can't figure out what have I've done wrong.
I'm calling the function like this in the main function:
addTree(&binST, tmp);
I've used the debugger and it gives me not a single error or warning.
Any help would be appreciated.
if (*bst == NULL) {
(*bst)->root = newNode(info); // <- Breaks the program
Excatly problem lies here , as *bst is NULL then in next line you dereference it (as you try to access struct member) which causes undefined behaviour and crash in your case .
You need to allocate memory to *bst before access members of the structure. Like this -
if (*bst == NULL) {
*bst=malloc(sizeof(BST)); //allocate memory first and then access struct members
(*bst)->root = newNode(info);
Note - Remember to free allocated memory.
Related
I'm fairly new to coding in c and have stumbled upon a problem. The code below is what I have so far and the problem at hand is how to looping through the node job_list.
We have two nodes, the first one creates a basic job post with a reference number (an integer) and job name (a string - an array of characters) as parameters, and the second is a list that holds job posts with the job_node being one parameter and then the second one being a standard struct job_list * next parameter.
I think you mixed few things up..
singly link list (that's what i understand you are up to) is not a fixed size data structure. it even not an increasing space data structure. link list use exactly the amount of memory you want to hold in it.
link list is a list of nodes. you can create list - and it will be an empty list.
then, you can add nodes with your desired data and every node you insert - create a new node (on the heap = dynamically allocated) which will contain your data and will be linked to the list.
every data node in the list will have a node_next it will point to, and node_prev which will point at this particular node(head and tail will not have this 2 nodes. head will point at node_next but will not have node_prev which point to it, and tail only have node_prev which will point to it).
so, if you want to create list you will have to dynamically allocate the space of the list which contain 2 nodes: head and tail(or end).
this is an example of create function:
slist_t *SlistCreate(void)
{
slist_t *list = malloc(sizeof(slist_t));
slist_iter_t dummy = NULL;
if(NULL == list)
{
return (NULL);
}
dummy = malloc(sizeof(node_t));
if(NULL == dummy)
{
free(list);
return (NULL);
}
dummy->next = NULL;
dummy->data = (void *)list;
list->head = dummy;
list->tail = dummy;
return (list);
}
then you will be able to insert nodes before particular node or after - that's depend on how you will implement this:
if you want to implement insert before:
you will have to encapsulate the list struct and prevent the user from sending your head to insert before.
or you can check every time if your insert function get the head of the list and update the head of the list (you can insert after and copy the head data to the head->next node and then use the head for the data the user wanted to be at the head of the list.
if you want to implement insert after:
you will have to check if the user sent you the tail (also called dummy_node cause it point to dummy_node->next = NULL), and use the same method i mentioned before, only the opposite way..
this is an example of insert before - in this example i used slist_iter_t which is pointer to node_t - the user isn't exposed to any of this structs (not to the list struct or the nodes struct):
slist_iter_t SlistInsert(slist_iter_t iterator, const void *data)
{
slist_t *ptr = NULL;
slist_iter_t dup = (slist_iter_t)malloc(sizeof(node_t));
if(NULL == dup)
{
return(NULL);
}
assert(NULL != iterator);
dup->data = iterator->data;
dup->next = iterator->next;
iterator->data = (void *)data;
iterator->next = dup;
if(NULL == dup->next)
{
ptr = (slist_t *)dup->data;
ptr->tail = dup;
}
return (iterator);
}
so, for using this DS you will have to write create function, insert function and destroy function (you will have to free all your dynamically allocated memory).
you may add more function such as remove, search data, clear list, is empty and so on. if you choose to encapsulate this implementation and hide from the user the struct of the list and the struct of the node, you will have to add more function such as get data function, get next node, and more..
you mentioned you need to insert if this data doesn't exist in the list so you can send from insert function to find function.
your function needs to look something like this:
struct job_list {
struct job_node * front;
struct job_list * next;
};
struct job_node {
int reference_number;
char * job_name;
struct job_node *next;
};
for your first function:
struct job_node *JobListCreate(void)
{
struct job_node *list = malloc(sizeof(struct job_node));
struct node_job dummy = NULL;
if(NULL == list)
{
return (NULL);
}
dummy = malloc(sizeof(node_t));
if(NULL == dummy)
{
free(list);
return (NULL);
}
dummy->next = NULL;
dummy->data = (void *)list;
list->head = dummy;
list->tail = dummy;
return (list);
}
for your second function:
void JobListInsertInFront(struct job_node *list, int reference_number, char * job_name)
{
slist_t *ptr = NULL;
struct node_job dup = NULL;
assert(NULL != list);
dup = (struct node_job)malloc(sizeof(node_t));
if(NULL == dup)
{
printf("Allocation failed\n");
return;
}
dup->reference_number = list->head->reference_number;
dup->job_name = list->head->job_name;
dup->next = list->head->next;
list->head->reference_number = reference_number;
list->head->job_name = job_name;
list->head->next = dup;
return;
}
and for the last function:
bool JobListInsertIfNotExist(struct job_node *list, int reference_number, char * job_name)
{
slist_t *ptr = NULL;
struct node_job dup = NULL;
assert(NULL != list);
while (NULL != dup)
{
if (dup->reference_number == reference_number && dup->job_name == job_name)
{
return false;
}
dup = dup->next;
}
dup = (struct node_job)malloc(sizeof(node_t));
if(NULL == dup)
{
printf("Allocation failed\n");
return;
}
dup->reference_number = list->head->reference_number;
dup->job_name = list->head->job_name;
dup->next = list->head->next;
list->head->reference_number = reference_number;
list->head->job_name = job_name;
list->head->next = dup;
return true;
}
As Jack Lilhammers pointed out in the comments, your code is very complex and there are a lot of mistakes in it, so I wrote down some general functions that you can then modify accordingly.
This is the basic struct, that we are going to work with:
struct node {
int data;
struct node *next;
};
Create a new node
Then this is how you'd create a new node:
#include <stdio.h>
#include <stdlib.h>
struct node *new_node(int data, struct node *next)
{
struct node *new = malloc(sizeof *new);
if (!new) {
printf("Error: memory allocation failed");
exit(EXIT_FAILURE);
}
new->data = data;
new->next = next;
return new;
}
You would initially call the function like this:
struct node *head = new_node(5, NULL);
Check if a node exists
Normally you would do something like this to check if a node with specific data exists in the linked list:
#include <stdbool.h>
/* Return whether or not the node exists */
bool node_exists(struct node *head, int data)
{
struct node *cursor = head;
while (cursor != NULL) {
if (cursor->data == data)
return true;
cursor = cursor->next;
}
return false;
}
Insert new node at the end
If you want to insert a new node at the end of the linked list, this is how it works:
void insert_last(struct node *head, struct node *new)
{
struct node **cursor = &head;
while ((*cursor) != NULL)
cursor = &(*cursor)->next;
*cursor = new;
}
Insert new node if nonexistent
You can combine the last two functions to only insert a new node at the end of the linked list, if the data doesn't already exist:
#include <stdbool.h>
/*
* Return whether or not the node exists. If it exists,
* insert the new node at the end of the linked list.
*/
bool new_insert_last(struct node *head, struct node *new)
{
struct node **cursor = &head;
while ((*cursor) != NULL) {
if ((*cursor)->data == new->data)
return true;
cursor = &(*cursor)->next;
}
*cursor = new;
return false;
}
This function could be called like this:
new_insert_last(head, new_node(3, NULL));
I have created a GitLab Snippet, so you can see the functions in action.
I've been learning C and am having problems using linked lists. When looping over a pointer to a linked list I run into segmentation faults and I'm not sure why.
Looking at similar questions the suggestion is to allocate the memory, but I find this answer confusing. Do you have to use heap memory for linked lists, and if so why?
Here is my code:
#include <stdio.h>
typedef struct Node {
char *name;
struct Node *next;
} Node;
typedef struct Thing {
Node *node;
} Thing;
Thing make_thing()
{
Thing t = {
.node = NULL
};
return t;
}
Thing * add_node(Thing *t, char *name)
{
Node node = {
.name = name,
.next = t->node
};
t->node = &node;
return t;
}
void print_nodes(Thing *t)
{
Node *n = t->node;
while(n != NULL) {
printf("Node: %s\n", n->name);
n = n->next;
}
}
int main()
{
printf("Start\n");
Thing t = make_thing();
add_node(&t, "one");
printf("First %s\n", t.node->name);
print_nodes(&t);
return 0;
}
You are using objects with automatic storage out of their scope:
Node node = {
.name = name,
.next = t->node
};
t->node = &node;
return t;
Here you leak the pointer &node, which is invalid (out of scope) after the return, to the caller and use it here:
printf("First %s\n", t.node->name);
You have to allocate memory by using malloc() for your Node structure.
Example:
Node *node = malloc(sizeof *node);
node->name = name;
node->next = t->node;
t->node = node;
return t;
You have to care about freeing the memory when it is no longer used to prevent memory leaks.
I've created a linked list were each node contains a structure as element and a pointer to the next node as follows
list.h
typedef struct node {
group data;
struct node *next;
} node;
typedef struct group {
unsigned int elements_count;
unsigned int closed;
unsigned int members[4];
} group;
list.c
node *add(node *head, group toadd) {
node *n_node = (node*) malloc(sizeof(node));
if(n_node != NULL) {
n_node->next = head;
group *n_group = &n_node->data;
/* Copy the values of the group into the created node */
n_group->elements_count = toadd.elements_count;
n_group->closed = toadd.closed;
for(int i = 0; i < 4; i++)
n_group->members[i] = toadd.members[i];
}
else {
throw_error("malloc returned a NULL pointer");
}
return n_node;
}
The problem arise when I try to read the first element of the array (node->data.members[0]).
Valgrind says that the problem is an invalid read of size 4 where the address is not stack'd, malloc'd or (recentrly) free'd.
Why am I getting a segmentation fault even if I have used malloc to allocate each node?
EDIT:
main.c
node *group_list = NULL;
/* Other code here.. */
group *cur_group = is_present(group_list, msg_gest.mtype);
if(cur_group == NULL) {
// The group isn't still present in the group list, then add it
group new_group = {
.elements_count = 0,
.closed = 0,
.members = {-1, -1 , -1, -1}
};
new_group.members[new_group.elements_count++] = msg_gest.mtype;
new_group.members[new_group.elements_count++] = msg_gest.to_add;
new_group.closed = msg_gest.to_close;
group_list = add(group_list, new_group);
} else {
cur_group->members[cur_group->elements_count++] = msg_gest.to_add;
cur_group->closed = msg_gest.to_close;
}
is_present
group* is_present(node *head, int matr) {
group *c_group;
node *c_node = head;
while(c_node != NULL) {
c_group = &c_node->data;
if(*(c_group->members) == matr) // !!Segmentation fault is caused by this read
return c_group;
printf("\n%d", *(c_group->members));
c_node = c_node->next;
}
return NULL;
}
I think that the problem was caused by an heap overflow, to solve it I've modified the node struct as follows
typedef struct node {
group* data;
struct node *next;
} node;
and I allocated the group within the add function like this
n_node->data = (group*) malloc(sizeof(group));
Try replacing the line
if(*(c_group->members) == matr)
With
if(c_group->members[0] == matr)
I am working on an a queue and keep running into problems with enqueuing. Here is what I believe to be the relevant code:
typedef struct Qnode QNODE;
struct Qnode
{
int length;
QNODE* next;
QNODE* prev;
};
typedef struct lqueue lQUEUE;
struct lqueue
{
QNODE *head;
QNODE *tail;
};
lQueue lqueue_init_default(void)
{
lQUEUE* pQ = NULL;
pQ = (lQUEUE*)malloc(sizeof(lQUEUE));
if (pQ != NULL)
{
pQ->head = NULL;
pQ->tail = NULL;
}
pQ->head = pQ->tail;
return pQ;
}
Status lqueue_henqueue(lQueue* hLQ, int lc)
{
lQUEUE* pLQ = (lQUEUE*)hLQ;
QNODE* new = (QNODE*)malloc(sizeof(QNODE));
if (new == NULL)
{
printf("Couldn't allocate space.\n");
return FAILURE;
}
new->length = lc;
new->next = pLQ->tail->next;
pLQ->tail = new;
return SUCCESS;
}
Whenever I try to run the program, I get this error during run time:
Exception thrown: read access violation.
pLQ->tail was nullptr.
Why is it a null pointer? Does it have to do with the Initialization function?
Here is how it is called:
int cl = 0;//Individual car length
lQueue hLQ = lqueue_init_default();//Handle to the left queue
printf("Enter the length of the lcar:\n");
scanf("%d", &cl);
lqueue_henqueue(hLQ, cl);
Your code is highly prone to undefined behavior... Look at this if statement:
if (pQ != NULL)
{
pQ->head = NULL; // This pointer is now 'NULL'
pQ->tail = NULL; // This is also 'NULL'
}
Which should be this...
if (pQ != NULL)
{
pQ->head = (QNODE*)calloc(1, sizeof(lQUEUE)); // This is proper pointer initialization...
pQ->tail = (QNODE*)calloc(1, sizeof(lQUEUE));
}
And this:
lQueue lqueue_init_default(void)
should be this:
lQueue * lqueue_init_default(void) // Since you are returning a pointer...
You will see that the code works fine because there is no undefined behavior...
Note that you can never access an object that is assigned to NULL... (Only if you don't want your program to behave undefined...) So, this:
pQ->tail = NULL;
is not safe in the very least... Structural pointers being assigned to NULL are usually only seen when being destroyed... An example is given below...
Also, unrelated, but have a destructor for the structure and call it when you don't need the structure anymore, or it will leak the memory afterwards...
void destroy_lqueue(struct lqueue ** queue)
{
if (queue != NULL)
queue = NULL;
free(queue);
}
I am working with a double linked list and I have run into a problem with my pop() function.
//QueueElement describe the block in the cache
typedef struct _queue_ele_
{
char *content; //the data of the block
struct _queue_ele_ *prev;
struct _queue_ele_ *next;
}QueueElement;
typedef struct _queue_
{
int queue_len;
int max_queue_size;
QueueElement *head;
QueueElement *tail;
}MyQueue;
The pop function works until there is an input of 2 elements ( I clear the queue by poping one by one and freeing the memory)
pop:
// head is removed and returned
QueueElement* pop(MyQueue* myqueue)
{
// if empty
if(myqueue->queue_len == 0) return NULL;
QueueElement *p = myqueue->head;
// if one element
if(myqueue->queue_len == 1)
{
myqueue->queue_len--;
myqueue->head = NULL;
myqueue->tail = NULL;
return p;
}
else
{
myqueue->queue_len--;
//remove the head from the queue
myqueue->head = myqueue->head->prev;
myqueue->head->next = NULL; //******************Seg Fault here
p->prev = NULL;
return p;
}
}
The error I get when there are two elements is a segmentation fault in line shown, but it works for queues with more. Why wont it let me assign NULL to myqueue->head->next???
Change this:
myqueue->head = myqueue->head->prev;
myqueue->head->next = NULL; //******************Seg Fault here
To:
myqueue->head = myqueue->head->prev;
if (myqueue->head != NULL) {
myqueue->head->next = NULL;
}
It is likely that you are trying to dereference a NULL pointer. It also would appear that you may have a memory leak from not calling free on the nodes you are deleting, but it is possible you do that elsewhere in the code.