4

I still have troubles with the relations between the linked lists and the structures.

See, my objectif is to create a list where each node contains 2 characters strings. So, I tried something like this : first, I create a structure that represent an element with my 2 char ; second, a control structure for my list, thath will point at the beginning of my list. Which, in my .h, gives something like this :

typedef struct s_def { char *first_word; char *second_word; struct s-def *next; }  t_def

typedef struct s_type { t_def *first; } t_list;

Next, I try to initialize my list. I make a function that work like this :

t_list *list;
t_def *words;

list = malloc(sizeof(*list));
words = malloc(sizeof(*words));
if (list == 0 || words == 0)
   return (NULL);
words = NULL;
words->next = NULL;
list->first = words;

return (list);

Precision : I try to make an empty list for now, so that the user can add some elements later.

And that's where it block : when I run the program, it gives the typical Segmentation Fault. But it don't see what's wrong with what I made ! I put some write in my function to retrace the process : the malloc are working ok, as well as the words = NULL, but then the segment fault seems to run at the line

words->next = NULL;

What do I make wrong ? Why can't I give a NULL value at the next of my words ?

3 Answers 3

5

You first initialize the word pointer with allocated memory

words = malloc(sizeof(*words));

Then 3 lines down you set that pointer to NULL again, creating a memory leak

words = NULL;

And then you try to dereference the pointer that you just set to NULL:

words->next = NULL;

So, just remove the words = NULL;

Sign up to request clarification or add additional context in comments.

1 Comment

Yeah !! I tried to remplace it by words->first_word = NULL; words->second_word = NULL and it passed !! Now, I have my empty list, my next difficulty will be to correctly make a if to check if it is empty and the fill it (though I have a segmentation fault here too). Anyway, thank you, nos !!
5

The problem is most likely this part:

words = NULL;
words->next = NULL;

Here you reassign the pointer words to be a null pointer, and directly afterwards you dereference this null pointer, leading to undefined behavior.

1 Comment

Yeah, that's it, and it give the value null to my words instead. Thanks !
1

When you set words to NULL, you have made a null pointer. Trying to access it immediately afterwards by words->next is effectively doing NULL->next which will cause an error. Your code looks a little more complex than it needs to be for a simple linked list implementation, you might try something like:

typedef struct s_element
{
    char* firstWord;
    char* secondWord;
    s_element* next;
} t_element;


t_element* list = NULL;

t_element* addFront(t_element* list, char* word1, char* word2)
{
    t_element* next = list;
    list = malloc(sizeof(t_element));
    if (!list) return NULL;
    list->firstWord = word1;
    list->secondWord = word2;
    list->next = next;
    return list;
}

Assuming I haven't made any bone-headed syntax mistakes, this should be about as clear as a linked list can get. Notice that it doesn't need to check if the list is empty, the only conditional is in case malloc has failed.

2 Comments

This is interesting ; I create 2 structures because I still have troubles with the idea of a list with many sub-values in each node. For example, I'm not sure how to configure the add_elem function. Like I said, newbie in C ! Thanks anyway
The easiest way to think about a linked list is as a collection of data with a single "next" value too. The "next" value is the link, everything else is contents. You do need to keep a pointer to the front of the list separately (otherwise you don't know where it starts!) In the example above I show how to access the multiple sub-values in the new node (it's exactly the same as any pointer referenced data structure). I believe your add_elem is the same as my addFront function. I hope this helps a little.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.