Pointer Mayhem

This is a discussion on Pointer Mayhem within the C Programming forums, part of the General Programming Boards category; I'm trying to write a program that uses linked-lists, the intent is to improve my understanding of pointers. I ran ...

  1. #1
    Registered User
    Join Date
    Sep 2006
    Posts
    5

    Pointer Mayhem

    I'm trying to write a program that uses linked-lists, the intent is to improve my understanding of pointers.
    I ran into a strange problem (sigh as usual) ....everytime i call my appendlist() function an element is added to the linked-list but if the function is called on the same line it overrides the previous element....can somebody please explain to me how this is happening???


    The Problem:
    Code:
    void appendlist(struct ll *curlist, void *datax){
            struct ll newlinkedlist =  newlist(datax); //local allocation problem??? something to do with stack/heap?!!
            struct ll *end = endlist(curlist);
            end->flink = &newlinkedlist;
            newlinkedlist.blink = end;
            newlinkedlist.flink = NULL;
    }
    The Method I call the Function:
    Code:
    int main(){
        struct ll alist = newlist("BEGIN"); //new element1 added
        
        int i,ii;
        for(i=0;i<100;i++){
            char *text = (char*)malloc(11);
            for(ii=0;ii<10;ii++){
                text[ii] = rand() &#37; 108+126;
            }
            text[ii] = 0;
            appendlist(&alist,text); //overrides element2
            appendlist(&alist,text); //overrides element3
        }
       
        appendlist(&alist,"goat"); //new element4 added
        appendlist(&alist,"pig"); //new
        appendlist(&alist,"fry"); //new
        appendlist(&alist,"shy"); //new
        appendlist(&alist,"ape"); //...
        printlist(&alist);
        return 0;
    }
    The Semi-Working Code:
    Code:
    #include <string.h>
    #include<stdlib.h>
    #include<stdio.h>
    struct ll {
           struct ll *blink;
           void *data;
           struct ll *flink;
    };
    
    struct ll newlist(void *datax){
            struct ll newlinkedlist;
            newlinkedlist.blink = NULL;
            newlinkedlist.data = datax;
            newlinkedlist.flink = NULL;
            return newlinkedlist;
    }
    struct ll *beglist(struct ll *list){
            struct ll temp = *list;
            while(temp.blink != NULL){
                 temp = *temp.blink;
            }
            if(temp.flink != NULL){
                temp = *temp.flink;
                return temp.blink;
            }
            else{
                 return list;
            }
    }
    
    struct ll *endlist(struct ll *list){
            struct ll temp = *list;
            while(temp.flink != NULL){
                 temp = *temp.flink;
            }
            if(temp.blink != NULL){
                temp = *temp.blink;
                return temp.flink;
            }
            else{
                 return list;
            }
    }
    
    void printlist(struct ll *list){
         struct ll *ptrfinder = beglist(list);
         int i = 0;
         while(ptrfinder != NULL){
            printf("%i:%s\n",i++,ptrfinder->data);
            ptrfinder = (ptrfinder->flink);
         }
    }
    
    void appendlist(struct ll *curlist, void *datax){
            struct ll newlinkedlist =  newlist(datax); //local allocation problem??? something to do with stack/heap?!!
            struct ll *end = endlist(curlist);
            end->flink = &newlinkedlist;
            newlinkedlist.blink = end;
            newlinkedlist.flink = NULL;
    }
    
    
    int main(){
        struct ll alist = newlist("BEGIN"); //new element1 added
        
        int i,ii;
        for(i=0;i<100;i++){
            char *text = (char*)malloc(11);
            for(ii=0;ii<10;ii++){
                text[ii] = rand() % 108+126;
            }
            text[ii] = 0;
            appendlist(&alist,text); //overrides element2
            appendlist(&alist,text); //overrides element3
        }
       
        appendlist(&alist,"goat"); //new element4 added
        appendlist(&alist,"pig"); //new
        appendlist(&alist,"fry"); //new
        appendlist(&alist,"shy"); //new
        appendlist(&alist,"ape"); //...
        printlist(&alist);
        return 0;
    }
    Last edited by thomas41546; 05-11-2008 at 01:01 AM.

  2. #2
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,422
    Code:
    void appendlist(struct ll *curlist, void *datax){
    	struct ll newlinkedlist =  newlist(datax); //local allocation problem??? something to do with stack/heap?!!
    	struct ll *end = endlist(curlist);
    	end->flink = &newlinkedlist;
    	newlinkedlist.blink = end;
    	newlinkedlist.flink = NULL;
    }
    The local variable newlinkedlist will destruct once the function ends, hence end->flink will point to garbage or unexistant data.
    Allocate your data with malloc and don't forget to FREE! The same goes for text inside main because you're leaking memory which is BAD.

    And for some general understanding:
    http://cpwiki.sourceforge.net/A_pointer_on_pointers

    Perhaps it will enlighten some.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Problem:
    Code:
    struct ll newlist(void *datax){
            struct ll newlinkedlist;
            newlinkedlist.blink = NULL;
            newlinkedlist.data = datax;
            newlinkedlist.flink = NULL;
            return newlinkedlist;
    };
    
    void appendlist(struct ll *curlist, void *datax){
            struct ll newlinkedlist =  newlist(datax); //local allocation problem??? something to do with stack/heap?!!
            struct ll *end = endlist(curlist);
            end->flink = &newlinkedlist;
            newlinkedlist.blink = end;
            newlinkedlist.flink = NULL;
    }
    What it is telling you is that you are using a pointer to a local version of that object. You need to use malloc() to create a block of memory which exists outside the local stack for that function.

    Example:
    Code:
    struct ll *newlist(void *datax){
            struct ll *newlinkedlist = malloc(sizeof(*newlinkedlist));
    
            if(newlinedlist != NULL)
            {
                    newlinkedlist->blink = NULL;
                    newlinkedlist->data = datax;
                    newlinkedlist->flink = NULL;
            }
            return newlinkedlist;
    };
    That should fix you up.

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    5
    I appreciate the help,
    local memory allocation is no longer as foreign to me.

    Updated Working Code:
    Code:
    #include <stdlib.h>
    #include <stdio.h>
    #include <string.h>
    struct ll {
           struct ll *blink;
           void *data;
           struct ll *flink;
    };
    
    struct ll *newlist(void *datax){
            struct ll *newlinkedlist = malloc(sizeof(*newlinkedlist)); //thanks master5001 ;)
            if(newlinkedlist != NULL){ 
                 newlinkedlist->blink = NULL;
                 newlinkedlist->data = datax;
                 newlinkedlist->flink = NULL;
            }
            return newlinkedlist;
    }
    struct ll *endlist(struct ll *list){
            struct ll temp = *list;
            while(temp.flink != NULL){
                temp = *temp.flink;
            }
            if(temp.blink != NULL){
                temp = *temp.blink;
                return temp.flink;
            }
            else{
                 return list;
            }
    }
    struct ll *beglist(struct ll *list){
            struct ll temp = *list;
            while(temp.blink != NULL){
                 temp = *temp.blink;
            }
            if(temp.flink != NULL){
                temp = *temp.flink;
                return temp.blink;
            }
            else{
                 return list;
            }
    }
    
    void printlist(struct ll *list){
         struct ll *ptrfinder = beglist(list);
         int i = 0;
         while(ptrfinder != NULL){
            printf("%i:%s\n",i++,ptrfinder->data);
            ptrfinder = (ptrfinder->flink);
         }
    }
    char *printposlist(struct ll *list,unsigned long int pos){
         struct ll *ptrfinder = beglist(list);
         unsigned long int i = 0;
         while(ptrfinder != NULL && i < pos){
            ptrfinder = (ptrfinder->flink);
            i++;
         }
         if(i != pos || ptrfinder == NULL){
              return "";
         }
         else{
              return (ptrfinder->data);
         }
    }
    signed long int findlist(struct ll *list, void *datax, unsigned int dbytes){
         signed long int i = 0;
         struct ll *ptrfinder = beglist(list);
         while(ptrfinder != NULL){
            if(memcmp( (ptrfinder->data),datax,dbytes) == 0){
                return i;
            }
            ptrfinder = (ptrfinder->flink);
            i++;
         }
         return -1;
    }
    
    void appendlist(struct ll *curlist, void *datax){
            struct ll *newlinkedlist =  newlist(datax); 
            struct ll *end = endlist(curlist);
            end->flink = newlinkedlist; //thanks Elysia
            newlinkedlist->blink = end;
            newlinkedlist->flink = NULL;
    }
    void removelist(struct ll *curlist, void *datax, unsigned int dbytes){
            struct ll *ptrfinder = beglist(curlist);
            struct ll *temp = NULL;
            while(ptrfinder != NULL){
                if(memcmp((ptrfinder->data),datax,dbytes) == 0){
                      if(ptrfinder->blink != NULL && ptrfinder->flink != NULL){
                          (ptrfinder->blink)->flink = ptrfinder->flink;
                          (ptrfinder->flink)->blink = ptrfinder->blink;
                      }
                      if(ptrfinder->blink == NULL && ptrfinder->flink != NULL){
                            (ptrfinder->flink)->blink = NULL;
                            *curlist = *ptrfinder->flink;
                            
                      }
                      if(ptrfinder->blink != NULL && ptrfinder->flink == NULL){
                            (ptrfinder->blink)->flink = NULL;
                      }
                      free(ptrfinder);
                      return;                            
                }
                ptrfinder = (ptrfinder->flink);
            }
    }
    /*
    TODO
    implement the following:
    insert(position,data) ...replace append with it
    remove(position,data)
    sort()
    */
    
    int main(){
        
        struct ll *alist = newlist("BEGIN");
        int i,ii;
        for(i=0;i<10;i++){
            char *text = (char*)malloc(2);
            for(ii=0;ii<1;ii++){
                text[ii] = rand() % 108+126;
            }
            text[ii] = 0;
            appendlist(alist,text); 
        }
       
        appendlist(alist,"goat"); 
        appendlist(alist,"pig");
        appendlist(alist,"fry");
        appendlist(alist,"shy");
        appendlist(alist,"ape");
        appendlist(alist,"........head");
        removelist(alist,"........head",strlen("........edad"));
        printlist(alist);
        //printf("%s\n",printposlist(alist,6));
        printf("%i\n",findlist(alist,"goat",strlen("goat")));
        //struct ll begin = *beglist(&alist);
        //struct ll end   = *endlist(&alist);
        //printf("x:%s\n",( (alist.flink)->flink)->data);
        //printf("b:%s\n", begin.data);
        //printf("e:%s\n", end.data);
        return 0;
        
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 03-24-2008, 10:16 AM
  2. Basic things I need to know
    By maxorator in forum C++ Programming
    Replies: 53
    Last Post: 10-15-2006, 04:39 PM
  3. Parameter passing with pointer to pointer
    By notsure in forum C++ Programming
    Replies: 15
    Last Post: 08-12-2006, 07:12 AM
  4. Direct3D problem
    By cboard_member in forum Game Programming
    Replies: 10
    Last Post: 04-09-2006, 03:36 AM
  5. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM

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