Thread: C dynamically linked list, printing & pointers...

  1. #1
    Registered User
    Join Date
    Feb 2017
    Posts
    2

    C dynamically linked list, printing & pointers...

    Hi, I'm new to the forum and have been trying to learn about dynamically linked lists. I'm having a problem with my code, adding nodes to a list seems to work but for some reason I can't manage to print (or to free memory for that matter). My thought is that there's something wrong with my pointers, but I can't put a finger on what and where I am going wrong.

    My guess is that there's a simple fix with pointers...

    Here's what I have gotten so far...


    Code:
    
    
    #include <stdio.h>
    #include <stdlib.h>
    #define SIZE_MAX  20
    #define BUFFER_MAX 256
    FILE *file;
    
    
    
    
    /*Stuct*/
    struct car {
    char name[SIZE_MAX];
    int year;
    struct car *next;
    };
    
    
    typedef struct car Car;
    
    
    int addToList(Car *head, char *, int);
    void printList(Car *head);
    void releaseMem(Car *head);
    
    
    /*functions*/
    int addToList(Car *head, char *name, int year ){
    
    
    struct car *nd;
    nd = malloc(sizeof(struct Car));
    strcpy(nd->name, name);
    nd->year = year;
    //printf("%s %d\n",nd->name,nd->year);
    nd->next = NULL;
    
    
    if (head!=NULL){
        head->next = nd;
    }
    return 1;
    }
    void printList(Car *head){
    
    
            Car * current;
            current =    head;;
    
    
         while (current!=NULL){
               printf("%s %d\n",current->name,current->year);
               current = current->next;
               }
    
    
         }
    
    
    void releaseMem(Car *head){
        Car * current = head;
    
    
            while (current != NULL) {
                    //printf("remove %s %d",current->name  ,current->year);
                    free(current);
                    current = current->next;
            }
    printf("Mem free'd.\n");
    
    
    }
    
    
    /*Main*/
    
    
    int main (int argc, char *argv[]){
    
    
    Car *head = NULL;
    int year = 100 ;
    char buffer [BUFFER_MAX];
    char name[SIZE_MAX];
    
    
    
    
    /*opening file*/
    file = fopen(argv[1], "r");
    if (file == NULL){
      printf("\n\tWARNING: No data found.\n");
      exit(1);
    }
    else{
        printf("Reading file %s. \n",argv[1]);
    }
    printf("Data read to list.\n");
    while(fgets(buffer, BUFFER_MAX,file)!= NULL){
      sscanf(buffer,"%s%d",name,&year);
        //printf("%s %d\n",string, year);
        addToList(head,name,year);
    
    
    }
    
    
    printList(head);
    releaseMem(head);
    return 0;
    }
    Last edited by Andy_17; 02-20-2017 at 08:15 AM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    1. addToList needs to return a new head pointer, when you add to an empty list.

    2. Your releaseMem function accesses out of bound memory
    free(current);
    current = current->next; // this is out of bounds, you just freed the memory

    You normally do something like
    temp = current->next;
    free(current);
    current = temp;
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Feb 2017
    Posts
    2
    Ok, thank you, thought that there was something like that I had missed. Honestly at this point I have no idea on how to fix the addToList function.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Like so
    Car *addToList(Car *head, char *name, int year );

    You pass in the current head, and it returns the new (possibly different) head.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 10-19-2015, 07:23 AM
  2. Trouble with printing linked list
    By ArtemisFowl2nd in forum C++ Programming
    Replies: 8
    Last Post: 05-02-2014, 01:04 PM
  3. Printing a linked list
    By popnfresh12321 in forum C Programming
    Replies: 5
    Last Post: 09-22-2012, 05:27 PM
  4. Printing a linked list
    By Jslam9 in forum C++ Programming
    Replies: 5
    Last Post: 12-14-2002, 06:08 PM
  5. I need a dynamically allocated linked list
    By Flex in forum C Programming
    Replies: 2
    Last Post: 03-06-2002, 02:28 PM

Tags for this Thread