Thread: Segmentation fault with input test file

  1. #16
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    OK, there's a bug here:

    Code:
    void add_student(int loopnum)
    {
        
        
        char unit_name[101];
        int low, student_ID;
        Position P;
        low=0;
        
        scanf("%d %s", &student_ID, unit_name);
    
        units[loopnum] = (unit_struct*) malloc(sizeof(unit_struct));
        units[loopnum]->name = malloc(sizeof(unit_name)+1);
       
        while(strcmp(units[low]->name, unit_name) !=0)
        {
           
           low++;
        }  
       
        if(units[low]->IDs == NULL)
        {
           units[low]->IDs = MakeEmpty(NULL);
           P = Header(units[low]->IDs);
        }
        
        InsertInOrder(student_ID, units[low]->IDs);
    
        printf ("AddStud: pointer P pointing to: %p\n", P);
    
        P = Advance(P);
                
    }
    The problem is that P is only assigned a value if (units[low]->IDs == NULL). If that condition is not true, P does not get assigned.

    In either case, you use P in the call : P = Advance(P); which is wrong!

    I put a printf statement in to display the contents of the pointer to prove it:

    Code:
    1
    a
    4
    1
    a
    AddStud: pointer P pointing to: 0x970c10
    4
    2
    a
    AddStud: pointer P pointing to: 0x0
    Segmentation fault (core dumped)
    Also, the Advance function does nothing for this variable as it's local to the add_student function. I'm not sure what you're trying to do with it.

    Have fun fixing......
    Last edited by Hammer; 04-28-2002 at 07:24 PM.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  2. #17
    Registered User
    Join Date
    Apr 2002
    Posts
    13
    ..but if i remove that if statement, then the list is just going to be emptied every time that function is called isn't it?

  3. #18
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Originally posted by bentles
    ..but if i remove that if statement, then the list is just going to be emptied every time that function is called isn't it?
    Memory allocated via calls to malloc will remain in existance until you free() it. You have to keep a pointer that points to the start of the list, but that can exist globally or be passed from function to function.

    I haven't checked to see if your memory management is correct or not!

    That particular call to the advance func certainly does nothing. P is declared within add_student, and is therefore destroyed when you leave that function. If it was containing a value you needed, you would have to either assign it to an external variable, or return it to the caller.

    As a quick fix, I run the prog without that line, and it appeared to work OK.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  4. #19
    Registered User
    Join Date
    Apr 2002
    Posts
    13
    I dont even need the p variable, its not serving any purpose whatsoever..

  5. #20
    Registered User
    Join Date
    Apr 2002
    Posts
    13
    ....doesnt fix the problem tho..

  6. #21
    Registered User
    Join Date
    Apr 2002
    Posts
    13
    Oh thank you lord 1!! I figured it out. Thanks Hammer ! I fixed it by making a global variable position P in the header and used the following: P=First(units[low]->IDs after the InsertInOrder call in add_student to move the position to the next position.
    WOOHOO!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sequential file program
    By needhelpbad in forum C Programming
    Replies: 80
    Last Post: 06-08-2008, 01:04 PM
  2. Need Help Fixing My C Program. Deals with File I/O
    By Matus in forum C Programming
    Replies: 7
    Last Post: 04-29-2008, 07:51 PM
  3. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 10:46 AM
  4. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM