Thread: Pointer problem with linked list creating blanks.

  1. #1
    Registered User
    Join Date
    Dec 2010
    Posts
    10

    Pointer problem with linked list creating blanks.

    Hi everyone,
    I'm trying to print an array of linked lists and I'm getting into a bit of a bind. Before I pass it to the print function I know the list has been populated properly since the function prints my array of linked lists in order however somewhere in the function it sets a head pointer to NULL which throws everything after that.

    Code:
    printSparse(matrix1,matrix1->rows);
    		printf("\n");
    		printSparse(matrix2,matrix2->rows);
    		printf("\n");
    Code:
    void printSparse (Matrix *m, int dimensionRow)
    {
      int i=0;
      Node** rowptr = m->rootnodes;
    
    for (int i=0; i<m->rows; i++)
    {   	
      while(rowptr[i] != NULL)
      {
        printf("%d,%d,%d\n",i,rowptr[i]->col,rowptr[i]->val);
        rowptr[i] = (rowptr[i])->next;
      }
    }
    
    }
    In case you need to know what my structs look like:
    Code:
    typedef struct Node
    {
    	int col,val; // No need to store row
    	struct Node *next; // Used to point to next node in the linked list.
    } Node;
    
    typedef struct Matrix
    {
    	int rows,cols; //Records of number of rows and columns required.
    	Node **rootnodes;//will fill this with rows later 
    } Matrix;


    The problem is that once printSparse has finished the array of pointers (rootnodes) seems to be blank.

    So running through GDB before printSparse call:
    print matrix->rootnodes[0] = Node * (memory address)
    print matrix->rootnodes[1] = Node * (memory address)
    etc..

    Afterwards I get:
    print matrix->rootnodes[0] = Node * (0x0)

    So I'm doing something stupid in my printSparse function but I can't figure out what. Obviously I'm setting a headpointer to NULL somewhere but I can't figure out how to restructure or do things different. I'm no a wizard c programmer I've been plugging away on the overall program for weeks trying to get to grips with it so please bear with me.

    Thanks all.

  2. #2
    Registered User
    Join Date
    Nov 2011
    Location
    Saratoga, California, USA
    Posts
    334
    You are directly altering rootnodes with the dereferenced assignment
    Code:
    rowptr[i] = (rowptr[i])->next;
    You are chewing off the head of each row until you get to the end - NULL. And all your allocated Nodes slip out your memory leak.

    Perhaps the easiest solution would be to introduce a Node * current and set it to rowptr[i] at the beginning of each outer loop iteration.

  3. #3
    Registered User
    Join Date
    Dec 2010
    Posts
    10
    Hmm I was trying to make rowptr the 'temporary pointer' hence " Node** rowptr = m->rootnodes;" since I'm aware altering the actual pointer will mean buggering everything up...
    Could you perhaps explain to me in more detail what I'm doing wrong or suggest some code? In the meantime I'll keep having a go.
    Thanks for your help,

  4. #4
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    EDIT: Altering the actual pointer will bugger things up, however if two things both point at the same memory, then altering the object one of them points to will also bugger up what the other points to. You are not buggering up m->rootnodes itself, but rather all of it's constituent Node pointers, e.g. m->rootnodes[0], m->rootnodes[1].

    rowptr points to an array of Node *, each of which is the actual head element of it's list. It points to the same array of Nodes as m->rootnodes, so changing rowptr[i] also changes m->rootnodes[i].

    Since your print loop traverses the list, stopping when it reaches the end (NULL), what is effectively happening is you keep moving m->rootnodes[i] to m->rootnodes[i]->next until m->rootnodes[i] is NULL. Thus, you have "chewed off" the head of each row.

    What I would recommend is something like:
    Code:
    void printSparse (Matrix *m, int dimensionRow)
    {
        int i=0;
        Node* n;
    
        for (int i=0; i<m->rows; i++)
        {
            for (n = m->rootnodes[i]; n != NULL; n = n->next)
            {
                printf("%d,%d,%d\n",i,n->col,n->val);
            }
        }
    }
    Last edited by anduril462; 02-08-2013 at 10:35 AM.

  5. #5
    Registered User
    Join Date
    Dec 2010
    Posts
    10
    Thanks so much for your response anduril462 - I totally understand where I'm going wrong now and have managed to completely fix the bug. I feel like I'm finally making some progress with C!
    Stay tuned for some more bugs over the next few days

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. i have problem creating a linked list
    By nik2 in forum C Programming
    Replies: 9
    Last Post: 02-17-2010, 11:32 PM
  2. Linked List, a pointer problem?
    By SkidMarkz in forum C Programming
    Replies: 3
    Last Post: 09-16-2008, 10:39 AM
  3. Problem with creating a singly linked list
    By philvaira in forum C++ Programming
    Replies: 15
    Last Post: 08-01-2007, 03:38 PM
  4. Problem Creating Linked List in Function
    By Stunner in forum C++ Programming
    Replies: 21
    Last Post: 07-25-2007, 02:56 AM
  5. Problem with creating linked list
    By bigzeppelin2k in forum C Programming
    Replies: 4
    Last Post: 12-18-2003, 03:35 PM