wrong linked list value

This is a discussion on wrong linked list value within the C Programming forums, part of the General Programming Boards category; hi all, I have a problem with linked lists. I have written this code : Code: //in main() Vlak *vl1; ...

  1. #1
    Registered User
    Join Date
    Feb 2005
    Posts
    61

    wrong linked list value

    hi all,

    I have a problem with linked lists.
    I have written this code :
    Code:
    //in main()
        Vlak *vl1;
        vl1 = (Vlak *)malloc(sizeof(Vlak));
        vl1->v1[0] = 1.0f;
        initVlakList();
        addVlakList(vl1);
    
    ...
    
    void initVlakList(){
        VlakList *v;
        Vlak *f, *l;
    
        v = (VlakList *)malloc(sizeof(VlakList));
        f = (Vlak *)malloc(sizeof(Vlak));
        l = (Vlak *)malloc(sizeof(Vlak));
    
        v->first = f;
        v->last = l;
    
        G->vlak = v;
    }
    
    ...
    
    void addVlakList(Vlak *v){
        Vlak *first, *last;
        first = G->vlak->first;
        last = G->vlak->last;
    
        if ((last == 0) && (first == 0)){
            last = v;
            first = v;
        }
        else {
            last->next = v;
            v->prev = last;
            last = v;
        }
    }
    Vlak and VlakList are defined like this :
    Code:
    typedef struct Vlak{
        struct Vlak *next;
        struct Vlak *prev;
    
        float v1[3], v2[3], v3[3], v4[3];
    }Vlak;
    
    typedef struct VlakList {
        struct Vlak *first;
        struct Vlak *last;
    }VlakList;
    and G contains a pointer to a VlakList : G->vlak .

    When I call this :
    value = G->vlak->first->v1[0];
    , it gives a wrong result (waaaaaaay to high).

    Does anyone know what the problem could be ?

    Thanks in advance.

  2. #2
    Registered User
    Join Date
    Mar 2003
    Posts
    143
    Um... where to start...
    initVlakList() creates a VlakList and two Vlaks (which are uninitialised) and makes these the first and last items in the VlakList. Basically that is your mistake. When you printed out G->vlak->first->v1[0] you weren't printing what you added but one of these uninitalied Vlaks. Do this instead:
    Code:
    /* make an empty list */
    void initVlakList()
    {
      VlakList *v;
    
      v = (VlakList *)malloc(sizeof(VlakList));
      v->first = NULL;
      v->last = NULL;
      G->vlak = v;
    }
    Also, in addVlakList() last and first were temporary copies of what you wanted to make changes to and so your list wasn't really being updated. (oh and use NULL rather than 0 for pointers). Updated it looks like this:
    Code:
    void addVlakList(Vlak *v)
    {
      if ((G->vlak->last == NULL) && (G->vlak->first == NULL)){
        G->vlak->last = v;
        G->vlak->first = v;
      }
      else {
        G->vlak->last->next = v;
        v->prev = G->vlak->last;
        G->vlak->last = v;
      }
    }
    ::edit:: oops, screwed up the code tags - making the update before anyone notices...
    DavT
    -----------------------------------------------

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,761
    Impossible to tell from the random snippets of code you've posted.

    One obvious thing is to stop casting the return result of malloc (see the FAQ)
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Registered User
    Join Date
    Feb 2005
    Posts
    61
    thanks DavT, that did it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reverse function for linked list
    By Brigs76 in forum C++ Programming
    Replies: 1
    Last Post: 10-25-2006, 11:01 AM
  2. Anyone good with linked list.....I am not....
    By chadsxe in forum C++ Programming
    Replies: 11
    Last Post: 11-10-2005, 02:48 PM
  3. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  4. Linked list with two class types within template.
    By SilasP in forum C++ Programming
    Replies: 3
    Last Post: 02-09-2002, 06:13 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21