1. Limited time only! Sign up for a free 30min personal tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Infinite loop iterating over a linked list

  1. Oct 9, 2015 #1
    Mod note: Added code tags
    1. The problem statement, all variables and given/known data

    in my main I am getting infinite loop iterating over a link list
    2. Relevant equations


    3. The attempt at a solution
    Code (C):
    /*
    * graph.c
    *
    *  Created on: Oct 8, 2015
    *      Author: danif
    */

    #include <stdio.h>
    #include "IntList.h"

    int main(void)
    {

        typedef struct IntListStruct* List;
        List l1=IntListCons(3,NULL);
        List l2=IntListCons(3,l1);
        List l3=IntListCons(4,l2);
        List Tester;

        Tester=l3;
        while(Tester!=NULL)
        {
            printf("%d\n",Tester->element);
            Tester=rest(Tester);
        }


    }


    /*
    * IntList.c
    *
    *  Created on: Oct 8, 2015
    *      Author: danif
    */

    #include "IntList.h"


    struct IntListStruct* IntListCons(int newb, struct IntListStruct *oldList)
    {
        struct IntListStruct newL;
        newL.element=newb;
        newL.pt=oldList;
        struct IntListStruct* pnewL= &newL;

        return pnewL;
    }

    int first(struct IntListStruct* L)
    {

        return L->element;
    }

    struct IntListStruct* rest(struct IntListStruct* L)
    {
        return L->pt;
    }
     
    Last edited by a moderator: Oct 9, 2015
  2. jcsd
  3. Oct 9, 2015 #2
    in the future please put all code inside ['code']['/code'] (without the '). Makes it easier to read.


    Also what is the problem you are having? What do you believe the program should do?
     
  4. Oct 9, 2015 #3
    I previously stated that the problem is an infinite loop, so how can I fix that?. I am just trying to display every element in the list.
     
  5. Oct 9, 2015 #4

    Mark44

    Staff: Mentor

    Are you using a debugger?

    Also, please show us your header file, IntList.h.
     
  6. Oct 9, 2015 #5
    /*
    * IntList.h
    *
    * Created on: Oct 8, 2015
    * Author: danif
    */

    #ifndef INTLIST_H_
    #define INTLIST_H_
    #include <stdio.h>
    typedef struct IntListStruct* IntList;
    struct IntListStruct* rest(struct IntListStruct *L);
    struct IntListStruct* IntListCons(int newb, struct IntListStruct *oldList);
    int first(struct IntListStruct* L);

    struct IntListStruct
    {
    IntList pt;
    int element;

    };


    #endif /* INTLIST_H_ */
     
  7. Oct 9, 2015 #6
    I forgot to replace InList with struct IntListStruct* in some of the prototypes, but that should be a minor point
     
  8. Oct 9, 2015 #7

    Mark44

    Staff: Mentor

    Please use code tags -- [ code ] at the top and [ /code ] at the bottom (no extra spaces). They make your code more readable.
     
  9. Oct 9, 2015 #8
    Thank you for the the advice. I will keep it in mind.
     
  10. Oct 9, 2015 #9

    Mark44

    Staff: Mentor

    I believe there is a bug in your IntListCons() function.
    Code (C):
    struct IntListStruct* IntListCons(int newb, struct IntListStruct *oldList)
    {
        struct IntListStruct newL;
        newL.element=newb;
        newL.pt=oldList;
        struct IntListStruct* pnewL= &newL;

        return pnewL;
    }
     
    The newL struct is a local that is allocated on the stack when this function is entered, and "dies off" when control returns to the caller. The pointer you're returning is the address of memory that is subject to change.
     
  11. Oct 9, 2015 #10
    Ok, so how can I create an struct that doesnt die once the control returns to the caller. I think that there is an explanation behind about extern and all of that types. Or I think that I have dynamic memory
     
  12. Oct 9, 2015 #11

    Mark44

    Staff: Mentor

    You can allocate memory dynamically on the heap (instead of the stack) using malloc() -- its prototype is in stdlib.h.
     
  13. Oct 9, 2015 #12
    I

    The solution to the problem is this

    IntList IntListCons(int newb, struct IntListStruct *oldList)
    {
    IntList pnewL= malloc(sizeof(struct IntListStruct));
    pnewL->element=newb;
    pnewL->pt=oldList;

    return pnewL;
    }

    can you explain more about dynamic memoey and stack memory?. Explain me how this code really works.
     
  14. Oct 9, 2015 #13

    Mark44

    Staff: Mentor

    PLEASE USE CODE TAGS!
     
  15. Oct 9, 2015 #14
    Without getting too technical about how the stack operates.
    When you create a variable inside of a function. The compiler assumes that the memory it allocates for it is only need so long as you haven't exited from the function. When it does exit from the function the compiler returns the memory to the computer and forgets about it.
    What generally does happen is that memory is used for something else and you've lost whatever you placed there.

    When you use malloc the compiler takes memory from somewhere else, and reserves it till the memory is freed using the free command.
    PROTIP everytime you add a malloc to a program you should immediately add a corresponding free and code to ensure all malloc'd memory is free'd. Good programming habit to get into early.
     
  16. Oct 9, 2015 #15
    So the code to release memory would look something like this

    IntList releaser=Tester;
    while(Tester!=NULL)
    {
    Tester=rest(Tester);
    free(releaser);
    releaser=Tester;
    }
     
    Last edited: Oct 9, 2015
  17. Oct 9, 2015 #16
     
  18. Oct 9, 2015 #17

    Mark44

    Staff: Mentor

    I'm sure several of us have comments about you last code. Speaking for myself, if you can't be convinced to make your code "pretty" with code tags, I don't feel like spending the time helping out.

    PLEASE USE CODE TAGS!
     
  19. Oct 9, 2015 #18

    rcgldr

    User Avatar
    Homework Helper

    This would be a bit less code:

    Code (Text):

    IntList releaser;
        while(Tester!=NULL)
        {
            releaser=Tester;
            Tester=rest(Tester);
            free(releaser);
        }
     
    If you're going to use typedefs for pointer types, you might want to consider prefixing them with a P if it's not obvious, like PNode for pointer to node, or just use Node * for pointer assuming that Node is a typedef for a struct.
     
    Last edited: Oct 9, 2015
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?
Draft saved Draft deleted



Similar Discussions: Infinite loop iterating over a linked list
  1. C++ Lists & Iterators (Replies: 24)

Loading...