I think the problem is, that you only work on the malloc'ed memory locally within the function.
You give add_client a pointer of type client. This is given by call by value, i.e. assigning the pointer a newly malloc'ed memory will not assign this memory to the pointer in the main function! Therefore your client *current is pointing to an undefined memory location even after call of add_client.
Better do this:
Furthermore: What are you willing to achieve with:
head = current;
current = head
?
This is just a pointer. It doesn't point to anything yet. The very next thing you do is use this invalid pointer: current->next = NULL; That could be trying to write to any memory whatsoever.
Do current=NULL; instead. A NULL head will mean an empty list. If you follow the logic in your function, it still works fine when head is NULL -- 'next' will be set to NULL, meaning end of list, and it won't crash because nothing uses the contents of 'head'.
Last edited by Corona688; 03-31-2011 at 05:00 PM..
if this makes a difference, the way i am doing the list is backwards, so when i want to add something to it, i create a new one in the function, then add the given one (which could be NULL or an actual list) to the next, then return the new one
To be honest, I did not understand your last sentence.
Anyways it does not make much difference whether you want a new entry to be the first one (after the head) or the last one. It is all just rearranged the next pointer. In the first case you rearrange the pointer from the head element to your newly created one and from there to the previously first, in the latter case you just traverse the list and change the last pointer form NULL to the new entry, making sure that the pointer in the newly created element points to NULL
Hi, I am programming in kernel, and I want to use a double linked list that holds infos that every process could access and modify THIS list. So, I suppose it is a 'global' variable since every process(thread) can reach it, I am wondering where to put it? by changing some of the kernel files? (1 Reply)
can someone provide an example of a struct linked list, where it has strings as its values, and then how do I check if a specific string (say called buffer) exists in the list of structs?
i dont understand how to make a copy of it to check with
this is what i have
... (0 Replies)
i have a linked list set up like
typedef struct client_list {
char *client_name;
int client_socket_fd;
struct client_list *next;
} client;
client *client_list=NULL;
before adding to the list i check if it already exists, only if it does not then i add
if (client_list==NULL... (1 Reply)