Search code examples
clinuxxubuntu

Memory allocation code error


I'm developing a client-server program and when and I'm trying to implement a user linked list using this structure:

typedef struct user {
    char username[50];
    int user_pid;
    struct user *next;
} user_list;

I'm trying figure out what's wrong with the code, because the compiler doesn't give me any error, but when I try to user to print the users list, it simply doesn't display anything.

AddUser function:

AddUser(user_list *head, req req)
{
    if(head == NULL)
    {
        head = malloc(sizeof(user_list));

        if (head == NULL) 
            fprintf(stdout,"[SERVER] Error memory allocation ");

        strcpy(head->username, req.str);
        head->user_pid = req.client_pid;
        head->next = NULL;
    }
    else
    {   
        user_list *current = head;

        while (current->next != NULL) 
            current = current->next;

        current->next = malloc(sizeof(user_list));
        strcpy(current->next->username, req.str);
        current->next->user_pid = req.client_pid;
        current->next->next = NULL;
    }
    num_users++;
}

Main Function (short version)

int Main()
{
struct request req;
struct answer ans;
user_list *head = NULL;

do{

    read(fifo_1, &req, sizeof(req)); // Read client request

    if(strcasecmp(req.str, "adduser") == 0) 
    {               
        AddUser(head, req);
        strcpy(ans.str, "User added with success! You're logged!");
    }

    if(strcasecmp(req.str, "users") == 0) // Print on the screen the users list
    {
        user_list *current = head;

        while (current != NULL) 
        {
            fprintf(stdout, "%s\n", current->username);
            fprintf(stdout, "%d\n", current->user_pid);
            current = current->next;
        }

    }
}while(strcmp(req.str,"exit") != 0);
}

Solution

  • Putting together what others have already pointed out in comments:

    1. Change main. Instead of

      int Main()
      

      use

      int main()
      
    2. The value of head, doesn't change in main when you change it in AddUser. Here's one solution. Return head from AddUser.

      user_list* AddUser(user_list *head, req req)
      {
          if(head == NULL)
          {
              head = malloc(sizeof(user_list));
      
              if (head == NULL) 
                  fprintf(stdout,"[SERVER] Error memory allocation ");
      
              strcpy(head->username, req.str);
              head->user_pid = req.client_pid;
              head->next = NULL;
          }
          else
          {   
              user_list *current = head;
      
              while (current->next != NULL) 
                  current = current->next;
      
              current->next = malloc(sizeof(user_list));
              strcpy(current->next->username, req.str);
              current->next->user_pid = req.client_pid;
              current->next->next = NULL;
          }
          num_users++;
          return head;
      }
      
    3. Capture the return value of AddUser in main. Instead of just

      AddUser(head, req);
      

      use

      head = AddUser(head, req);