Thread: linked list and damn on linked list

  1. #1
    life is a nightmare
    Join Date
    Apr 2007
    Posts
    127

    linked list and damn on linked list

    hi all
    i really hate this damn thing
    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    
    struct info
    {
            int x;
            struct info *next;
    };
    
    int main()
    {
            struct info *root;
            struct info *new;
            int v=0;
            root=malloc(sizeof(struct info));
            new->next=malloc(sizeof(struct info));
            new=new->next;
            while(new!=0)
            {
                    new->x=5;
                    new->next=malloc(sizeof(struct info));
                    new=new->next;
                    v++;
                    if(v==5)
                    {
                            new->next=0;
                            new=new->next;
                    }
            }
    
            new=root;
            while(new!=0)
            {
                    printf("%d\n",new->x);
                    new=new->next;
            }
    return 0;
    }
    it keep telling me
    Segmentation fault
    it should initialize x to 5 for 5 times

    my question is
    why it's not working ?

  2. #2
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    This line:
    new->next=malloc(sizeof(struct info));

    "new" has nothing allocated
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  3. #3
    life is a nightmare
    Join Date
    Apr 2007
    Posts
    127
    new->next=malloc(sizeof(struct info));
    new=new->next;


    new is pointing to the allocated space
    i can't understand it

  4. #4
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Where is new pointing to allocated space? What are you smoking?
    If you understand what you're doing, you're not learning anything.

  5. #5
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    root->next is not initialized. all the new allocations are leaked.

  6. #6
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    look first of all you shouldn't have used a 'new' word as your variable. Since it is a keyword, but anyway.

    You can see that new has been declared as a pointer of type struct info * but not as a struct info. Which mean it can hold just the address of the struct info. So when you do this

    Code:
    new->next
    The new is no where pointing but you still dereference the struct elements which is not right and which is giving u SF.

    Allocate memory for new first and then dereference the new. This would give you some progress.

    ssharish2005

  7. #7
    life is a nightmare
    Join Date
    Apr 2007
    Posts
    127
    Quote Originally Posted by ssharish2005 View Post
    look first of all you shouldn't have used a 'new' word as your variable. Since it is a keyword, but anyway.

    You can see that new has been declared as a pointer of type struct info * but not as a struct info. Which mean it can hold just the address of the struct info. So when you do this

    Code:
    new->next
    The new is no where pointing but you still dereference the struct elements which is not right and which is giving u SF.

    Allocate memory for new first and then dereference the new. This would give you some progress.

    ssharish2005
    you mean for example
    [code]
    new= malloc(sizeof(struct info));
    new->next=new;
    head=new
    [code]

  8. #8
    life is a nightmare
    Join Date
    Apr 2007
    Posts
    127
    Quote Originally Posted by itsme86 View Post
    Where is new pointing to allocated space? What are you smoking?
    nothing i wish i can smoke

  9. #9
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    Code:
    new= malloc(sizeof(struct info));
    new->next=new; // you should perhaps need new->next=NULL, other wise u will end up 
    with the infinite loop when u print
    head=new
    ssharish2005

  10. #10
    life is a nightmare
    Join Date
    Apr 2007
    Posts
    127
    how about this one
    Code:
    #include<stdio.h>
    #include<string.h>
    #include<stdlib.h>
    
    struct info
    {
            int x;
            struct info *next;
    }h;
    int main()
    {
            struct info *head;
            struct info *current;
            int g=0;
            head=&h;
    
            current=malloc(sizeof(struct info));
            current->next=current;
            while(current!=0)
            {
                    current->x=5;
                    current->next=malloc(sizeof(struct info));
                    current=current->next;
                    g++;
                    if(g==5)
                    {
                            current->next=0;
                            current=current->next;
                    }
    
            }
    current=head;
    
    while(current!=0)
    {
            printf("&#37;s\n",current);
            current=current->next;
    }
    return 0;
    }
    but it's not printing any value just a new line

  11. #11
    life is a nightmare
    Join Date
    Apr 2007
    Posts
    127
    Quote Originally Posted by ssharish2005 View Post
    Code:
    new= malloc(sizeof(struct info));
    new->next=new; // you should perhaps need new->next=NULL, other wise u will end up 
    with the infinite loop when u print
    head=new
    ssharish2005
    i will try it now
    thanks for your help

  12. #12
    life is a nightmare
    Join Date
    Apr 2007
    Posts
    127
    daaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaamn
    it finally works
    Code:
    #include<stdio.h>
    #include<string.h>
    #include<stdlib.h>
    
    struct info
    {
            int x;
            struct info *next;
    }h;
    int main()
    {
            struct info *head;
            struct info *current;
            int g=0;
    
            current=malloc(sizeof(struct info));
            current->next=current;
            head=current;
            while(current!=0)
            {
                    current->x=5;
                    current->next=malloc(sizeof(struct info));
                    current=current->next;
                    g++;
                    if(g==5)
                    {
                            current->next=0;
                            current=current->next;
                    }
    
            }
    current=head;
    
    while(current!=0)
    {
            printf("&#37;d\n",current->x);
            current=current->next;
    }
    return 0;
    }
    thanks ssharish2005

  13. #13
    life is a nightmare
    Join Date
    Apr 2007
    Posts
    127
    i guess i finally under stand it
    current is a pointer from type struct *info
    we allocate space for this pointer to hold the data in the struct
    then we
    current->next=current witch points to the first data in the structure
    then we
    head=current
    to make head points to the first memory location for the list

    |IS this Right ??| if not would somebody pleas fix it
    thanks all

  14. #14
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Your description is right, but your logic is flawed. What would head->next end up pointing to? head. This makes an infinite loop which is probably not what you want (there are some applications for that, but probably not this instance).

    Instead of doing:
    Code:
    current->next = current;
    Try this:
    Code:
    current->next = NULL;
    If you understand what you're doing, you're not learning anything.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. circular doubly linked list help
    By gunnerz in forum C++ Programming
    Replies: 5
    Last Post: 04-28-2007, 08:38 PM