Segmentation Fault?

This is a discussion on Segmentation Fault? within the C Programming forums, part of the General Programming Boards category; Hey guys, I'm working on my programming assignment. I think everything is fine and dandy, and when I try to ...

  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    26

    Segmentation Fault?

    Hey guys, I'm working on my programming assignment. I think everything is fine and dandy, and when I try to compile and run the program it crashes. Then I use the debugger, and get an error that prevents the debugger from working correctly I suppose. I'm not really sure what it is, but it's a segmentation fault apparently.

    The whole program is supposed to read from an input file that's formatted as:

    n (number of cases)
    Last First (names)
    GBAnalyzed
    planetsDiscovered
    etc...

    The planets have different types depending on the number; and if there is a planet that had been discovered, the x,y, and z coordinates will be next to it.

    I don't know much more explaining that I could do, if there's a question I'd be happy to explain details of the assignment in detail but I really just want to find out what this segmentation fault is.

    Also, I apologize for the lengthy post in advance, and thank you to anyone that can help.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <math.h>
    
    
    typedef struct GeocentricCelestialReferenceFrame{
        int x;
        int y;
        int z;
    }GCRF;
    
    
    typedef struct DiscoveredPlanet{
        int usersConfirmed;
        int type;
    
    
        GCRF *coordinates;
        struct DiscoveredPlanet *next;
    }DiscPlanet;
    
    
    typedef struct VerifiedPlanet{
        int type;
        GCRF*coordinates;
        struct VerifiedPlanet *next;
    } VerPlanet;
    
    
    typedef struct UserWorkCompleted{
        char firstName[20];
        char lastName[20];
        int gbAnalyzed;
        int planetsDiscovered;
        struct UserWorkCompleted *next;
    }UserWork;
    
    
    typedef enum{
        NONE = 0,
        GAS = 1,
        ICE = 2,
        STORM = 3,
        BARREN = 4,
        TEMPERATE = 5,
        LAVA = 6,
        OCEANIC = 7,
        PLASMA = 8,
        UNKNOWN = 9
    }Planets;
    
    
    UserWork *add(char *, char *, int, int, int, int, int, UserWork *, DiscPlanet *, VerPlanet *);
    void addDiscPlanet(int, int, int, int, DiscPlanet *, VerPlanet *);
    void addVerPlanet(int, int, int, int, DiscPlanet *, VerPlanet *);
    
    
    int main()
    {
        FILE *fin;
        int numElements, planet, GB, x1 = 0, y1 = 0, z1 = 0;
        char lname[20], fname[20];
        UserWork *head;
        DiscPlanet *discHead;
        VerPlanet *VerHead;
        VerHead = (VerPlanet *)malloc(sizeof(VerPlanet));
        discHead = (DiscPlanet *)malloc(sizeof(DiscPlanet));
        head = (UserWork *)malloc(sizeof(UserWork));
        head = NULL; discHead = NULL; VerHead = NULL;
        fin = fopen("GridComputing.in", "r");
        if(fin == NULL)
        {
            printf("File could not be found.");
            return 1;
        }
        fscanf(fin, "%d ", &numElements);
        while(!feof(fin))
        {
            fscanf(fin, "%s ", lname);
            fscanf(fin, "%s ", fname);
            fscanf(fin, "%d ", &GB);
            fscanf(fin, "%d ", &planet);
            if(planet > 0)
            {
                fscanf(fin, "%d %d %d", &x1, &y1, &z1);
            }
            head = add(lname, fname, GB, planet, x1, y1, z1, head, discHead, VerHead);
        }
    
    
        UserWork *curr;
        DiscPlanet *currDisc;
        VerPlanet *currVer;
        curr = head;
        currDisc = discHead;
        currVer = VerHead;
        printf("Users:\n");
        while(curr->next!=NULL)
            printf("\t%s, %s - Analalyzed:%d Discovered:%d\n", curr->lastName, curr->firstName, curr->gbAnalyzed, curr->planetsDiscovered);
        printf("\nDiscovered Planets:\n");
        while(currDisc->next!=NULL)
            printf("\t(%d,%d,%d) - Type:%d\n", currDisc->coordinates->x, currDisc->coordinates->y, currDisc->coordinates->z, currDisc->type);
        printf("\nVerified Planets:");
        while(currVer->next !=NULL)
            printf("\t(%d,%d,%d) - Type:%d\n", currVer->coordinates->x, currVer->coordinates->y, currVer->coordinates->z, currVer->type);
    
    
        return 0;
    }
    
    
    UserWork* add(char *lname, char *fname, int GB, int planet, int x1, int y1, int z1, UserWork*head, DiscPlanet *discHead, VerPlanet *VerHead)
    {
        UserWork *curr, *append;
        curr = head;
        if(head == NULL)
        {
            append = (UserWork *)malloc(sizeof(UserWork));
            append->next = NULL;
            strcpy(append->firstName, fname);
            strcpy(append->lastName, lname);
            if(planet > 0)
            {
                append->planetsDiscovered = 1;
                addDiscPlanet(planet, x1, y1, z1, discHead, VerHead);
                return head;
    
    
            }
            else
                append->planetsDiscovered = 0;
    
    
        }
        while(curr->next!=NULL)
        {
            if(strcmp(lname, curr->lastName) == 0)
            {
                if(strcmp(fname, curr->firstName) == 0)
                    curr->gbAnalyzed+=GB;
                else if(strcmp(fname, curr->firstName) > 0)
                {
                    append = (UserWork *)malloc(sizeof(UserWork));
                    append->next = curr->next;
                    curr->next = append;
                    strcpy(append->firstName, fname);
                    strcpy(append->lastName, lname);
                    if(planet > 0)
                    {
                        append->planetsDiscovered = 1;
                        addDiscPlanet(planet, x1, y1, z1, discHead, VerHead);
                        return head;
    
    
                    }
                    else
                        append->planetsDiscovered = 0;
                }
                else if(strcmp(fname, curr->firstName) < 0)
                {
                    append = (UserWork *)malloc(sizeof(UserWork));
                    append->next = curr;
                    curr = append;
                    strcpy(append->firstName, fname);
                    strcpy(append->lastName, lname);
                    if(planet > 0)
                    {
                        append->planetsDiscovered = 1;
                        addDiscPlanet(planet, x1, y1, z1, discHead, VerHead);
                        return head;
                    }
    
    
                }
            }
        }
        return head;
    }
    
    
    void addDiscPlanet(int planet, int x1, int y1, int z1, DiscPlanet *discHead, VerPlanet *VerHead)
    {
    
    
         struct DiscoveredPlanet *curr, *next;
         if(discHead == NULL)
         {
            discHead->coordinates->x = x1;
            discHead->coordinates->y = y1;
            discHead->coordinates->z = z1;
            discHead->type = planet;
            discHead->usersConfirmed = 1;
            discHead->next = NULL;
        }
        else if(discHead != NULL)
        {
            curr = discHead;
            while(curr->next!=NULL)
            {
                if(curr->coordinates->x == x1 && curr->coordinates->y == y1 && curr->coordinates->z == z1 && curr->type == planet)
                {
                    curr->usersConfirmed+=1;
                    if(curr->usersConfirmed > 2)
                        addVerPlanet(planet, x1, y1, z1, discHead, VerHead);
                    return;
                }
                curr = curr->next;
            }
            next = (DiscPlanet *)malloc(sizeof(DiscPlanet));
            curr->next = next;
            next->next = NULL;
        }
    
    
    
    
    }
    
    
    void addVerPlanet(int planet, int x1, int y1, int z1, DiscPlanet *discHead, VerPlanet *VerHead)
    {
        VerPlanet *curr, *append;
        DiscPlanet *curr1, *del;
        int distanceList, distanceNew;
        distanceNew = sqrt((x1*x1)+(y1*y1)+(z1*z1));
        if(VerHead == NULL)
        {
            VerHead->type = planet;
            VerHead->coordinates->x = x1;
            VerHead->coordinates->y = y1;
            VerHead->coordinates->z = z1;
        }
        else if(VerHead!=NULL)
        {
            curr = VerHead;
            while(curr->next!=NULL)
            {
                distanceList = sqrt((curr->coordinates->x)*(curr->coordinates->x) + (curr->coordinates->y)*(curr->coordinates->y) +
                                    (curr->coordinates->z)*(curr->coordinates->z));
    
    
                if(distanceNew < distanceList)
                {
                    append = (VerPlanet *)malloc(sizeof(VerPlanet));
                    append->type = planet;
                    append->coordinates->x = x1;
                    append->coordinates->y = y1;
                    append->coordinates->z = z1;
                    append->next = curr->next;
                    curr = append;
                    return;
                }
                else
                {
                    append = (VerPlanet *)malloc(sizeof(VerPlanet));
                    append->type = planet;
                    append->coordinates->x = x1;
                    append->coordinates->y = y1;
                    append->coordinates->z = z1;
                    curr->next = append;
                    append->next = NULL;
                    return;
                }
            }
    
    
            append = (VerPlanet *)malloc(sizeof(VerPlanet));
            append->type = planet;
            append->coordinates->x = x1;
            append->coordinates->y = y1;
            append->coordinates->z = z1;
            curr->next = append;
            append->next = NULL;
        }
    
    
        curr1 = discHead;
    
    
        while(curr1->next!=NULL)
        {
            if(discHead->coordinates->x == x1 && discHead->coordinates->y == y1 && discHead->coordinates->z == z1 && discHead->type == planet)
            {
                discHead = curr1->next;
                free(curr1);
                curr1 = curr1->next;
            }
            else if(curr1->coordinates->x == x1 && curr1->coordinates->y == y1 && curr1->coordinates->z == z1 && curr1->type == planet)
            {
                del->next = curr1->next;
                free(curr1);
            }
            del = curr1;
            curr1 = curr1->next;
        }
    
    
    }

  2. #2
    Registered User
    Join Date
    Dec 2011
    Posts
    795
    You don't need to assign every value when they're sequential in an enum. It will automatically do that.

    Find the offending code more specifically (don't dump 299 lines and expect us to fix it), by using GDB or a similar debugger.

  3. #3
    Registered User
    Join Date
    Sep 2011
    Posts
    26
    Well, I did do that, but didn't want there to be any confusion as to what I was doing. The problem is at line 136 and 88. I'm not sure why, though.

  4. #4
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,288
    The code on line 136 would seg fault if curr was not pointing to a valid memory location.
    Well as it happens on line 116 curr could certainly be NULL. You test for head being NULL on line 117, which was just assigned to curr, so you appear to already be expecting that possibility.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  5. #5
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,648
    The key to fixing seg faults:

    1. Compile the code in debug mode.
    2. Run the code in the debugger.
    3. When the seg fault occurs, execution stops.
    4. Evaluate variables at time of seg fault.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C Segmentation Fault Help
    By cow_tipper8282 in forum C Programming
    Replies: 5
    Last Post: 10-16-2010, 03:25 PM
  2. Segmentation Fault
    By daniel-lemke in forum C Programming
    Replies: 3
    Last Post: 10-15-2010, 12:03 PM
  3. Segmentation fault
    By (Slith++) in forum C++ Programming
    Replies: 4
    Last Post: 05-06-2007, 03:47 AM
  4. Segmentation Fault
    By ankurcse in forum C Programming
    Replies: 11
    Last Post: 09-27-2006, 10:26 AM
  5. segmentation fault and memory fault
    By Unregistered in forum C Programming
    Replies: 12
    Last Post: 04-02-2002, 10:09 PM

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