Thread: An access violation (segmentation fault) was raised in your program. HELP!

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    2

    An access violation (segmentation fault) was raised in your program. HELP!

    Hi all i have completed my c program but i receive the error "An access violation (segmentation fault) was raised in your program." When debugging in Dev c++ it fails to go past the int main() and brings up this error. I cant seem to find where the error is. Help would be greatly appreciated.

    Code:
    #include <stdio.h>
    #include <string.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;
    
    //Declare userLists 
    userWork *userList = NULL;
    discPlanet *discList = NULL;
    verPlanet *verList = NULL;
    userWork *currentUser = NULL;
    discPlanet *currentDisc = NULL;
    verPlanet *currentVer = NULL;
    
    userWork *insertUser(userWork *userList, char *first, char *last);
    discPlanet *confirmedPlanets(discPlanet* listDisc, verPlanet* listVer, int typePlanet, int x, int y, int z);
    
    int main(){
        int entries, i, analyzed, discovered, x, y, z;
        char first, last;
        
        
        
        //Scan in input file and number of entries
        FILE *fp;
        fp = fopen("GridComputing.in","r");
        fscanf(fp, "%d" , &entries);
    
    //Initialize for loop for number of entries
        for(i=0; i<entries; i++){
       
            fscanf(fp, "%s %s", first, last);
            fscanf(fp, "%d", &analyzed);
            fscanf(fp, "%d", &discovered); 
            
            currentUser = insertUser(userList, &first, &last);
            currentUser->gbAnalyzed = currentUser->gbAnalyzed + analyzed;
            
            if(discovered != 0){
                currentUser->planetsDiscovered++;
                fscanf(fp, "%d %d %d", &x, &y, &z); 
                currentDisc = confirmedPlanets(discList, verList, discovered, x, y, z);
            }
                
        }
        fclose(fp);
            
        fp = fopen("GridComputing.out", "w");
        
        fprintf(fp, "Users:\n");
        currentUser = userList;
        
        userWork* userTemp;
        while(currentUser != NULL){
            fprintf(fp, "\t%s, %s - Analyzed:%d\n", currentUser->lastName, currentUser->firstName, currentUser->planetsDiscovered);
            userTemp = currentUser;
            currentUser = currentUser->next;
            free(userTemp);
        }
        
        fprintf(fp, "\nDiscovered Planets:\n");
        currentDisc = discList;
        
        discPlanet* discTemp;
        while(currentDisc != NULL){
            
            fprintf(fp, "\t(%d,%d,%d) - Confirmed:%d Type:%d\n", currentDisc->coordinates->x, currentDisc->coordinates->y, currentDisc->coordinates->z, currentDisc->usersConfirmed, currentDisc->type);
            discTemp = currentDisc;
            currentDisc = currentDisc->next;
            free(discTemp->coordinates);
            free(discTemp);
        }
        
        fprintf(fp, "\nVerified Planets:\n");
        currentVer = verList;
        
        verPlanet* verTemp;
        while(currentVer != NULL){
            
            fprintf(fp, "\t(%d,%d,%d) - Type:%d\n", currentVer->coordinates->x, currentVer->coordinates->y, currentVer->coordinates->z, currentVer->type);
            verTemp = currentVer;
            currentVer = currentVer->next;
            free(verTemp->coordinates);
            free(verTemp);
        }
        
        
        fclose(fp);
        
        return 0;
    }
    
    
    userWork *insertUser(userWork *userList, char *first, char *last) {
    
      userWork *temp;
      userWork *current;
    
    
      // Create the new node.
      temp = (userWork*)malloc(sizeof(userWork));
      temp->next = NULL;
    
      // Take care of case inserting into an empty userList.
      if (userList == NULL){
             temp=(userWork*)malloc(sizeof(userWork));
             temp->next = NULL;
             
             strcpy(temp->firstName, first);
             strcpy(temp->lastName, last); 
             
             temp->gbAnalyzed = 0; 
             
             temp->planetsDiscovered = 0; 
             
             userList=temp;
             
             return temp;
    }
    
    if(stricmp(last, userList->lastName)== 0){
            
            if(stricmp(first, userList->firstName) == 0){
                return userList;
            }
            
            else if(stricmp(first, userList->firstName)< 0){
                temp=(userWork*)malloc(sizeof(userWork));
                strcmp(temp->firstName, first);
                strcmp(temp->lastName, last);
                temp->gbAnalyzed = 0;
                temp->planetsDiscovered = 0; 
                
                
                temp->next = userList; 
                
                userList = temp;
                
                return temp;    
            }
        }
        else if(stricmp(last, userList->lastName) < 0){
            temp=(userWork*)malloc(sizeof(userWork));
            strcmp(temp->firstName, first);
            strcmp(temp->lastName, last); 
            
            temp->gbAnalyzed = 0;
            temp->planetsDiscovered = 0; 
            
            temp->next = userList; 
            
            userList = temp;
            
            return temp;
        }
    
        while(current->next != NULL) {
        
            if(stricmp(first, current->next->firstName) == 0){
                
                if(stricmp(last, current->next->lastName) == 0){
                    return current->next;
                }
            
                else if(stricmp(first, current->next->firstName) < 0){
                    temp=(userWork*)malloc(sizeof(userWork));
                    strcmp(temp->firstName, first);
                    strcmp(temp->lastName, last);
                    temp->gbAnalyzed = 0;
                    temp->planetsDiscovered = 0;
                    temp->next = current->next;
                    current->next = temp;
                
                    return temp;
                }
            }
            else if(stricmp(last, current->next->lastName) < 0){
                temp=(userWork*)malloc(sizeof(userWork));
                strcpy(temp->firstName, first);
                strcpy(temp->lastName, last);
                temp->gbAnalyzed = 0;
                temp->planetsDiscovered = 0; 
                temp->next = current->next; 
                
                current->next = temp; 
                
                return temp;
            }
            
            current = current->next;
        }
        
        temp=(userWork*)malloc(sizeof(userWork));
        strcpy(temp->firstName, first);
        strcpy(temp->lastName, last);
        temp->gbAnalyzed = 0; 
        temp->planetsDiscovered = 0; 
        temp->next = NULL;
        current->next = temp; 
        return temp; 
    }
    
    discPlanet* confirmedPlanets(discPlanet *listDisc, verPlanet *listVer, int typePlanet, int x, int y, int z){
        discPlanet *dpNew = NULL;
        
        if(listDisc == NULL){
            listDisc=(discPlanet*)malloc(sizeof(discPlanet));
            listDisc->usersConfirmed = 1;
            listDisc->type = typePlanet;
            listDisc->coordinates = (GCRF*)malloc(sizeof(GCRF));
            listDisc->coordinates->x = x;
            listDisc->coordinates->y = y;
            listDisc->coordinates->z = z;
            listDisc->next = NULL; 
            return listDisc;
        }
        
        discPlanet* temp = (listDisc);
        discPlanet* temp2 = (listDisc);
        
        while(temp != NULL){
            if((temp->coordinates->x == x) && (temp->coordinates->y == y) && (temp->coordinates->z == z)){
                temp->usersConfirmed++;
                
                if(temp->usersConfirmed >= 3){
                    return NULL;
                }
            return temp;
            }
            if(temp->next == NULL){ 
                break;
            }
            temp = temp->next;
        }
        
        temp2 = (discPlanet*)malloc(sizeof(discPlanet));
        temp2->usersConfirmed = 1;
        temp2->type = typePlanet;
        temp2->coordinates = (GCRF*)malloc(sizeof(GCRF));
        temp2->coordinates->x = x;
        temp2->coordinates->y = y;
        temp2->coordinates->z = z;
        temp2->next = NULL;
        temp->next = temp2;
        return temp2;
    }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Time to learn how to debug. Get yourself a debugger, and/or start adding printf statements around to narrow down where it's crashing.

    You should have started testing small pieces one at a time, instead of waiting until you had written 300 lines before compiling.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Oct 2011
    Posts
    2
    i was compiling the whole time and didnt get the error until i finished the program. Iv tried debugging but cant seem to find where it is crashing. What is the best way to debug?

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by wo0dy View Post
    i was compiling the whole time and didnt get the error until i finished the program.
    So, it worked until you put in the last bit... Apply head to problem.... which section did you work on last?

    As far as debugging goes, that varies from OS to OS and IDE to IDE... so we're going to need a bit more information to give you a good answer.

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Compiling doesn't mean testing. I've already told you: Use a debugger, or start scattering printf statements around. You have to have some idea of what's going on before it crashes.


    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by wo0dy View Post
    i was compiling the whole time and didnt get the error until i finished the program. Iv tried debugging but cant seem to find where it is crashing. What is the best way to debug?
    As Quzah points out .... "Compiles" absolutely does not mean "Works"... it only means you didn't tick off the compiler.

  7. #7
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    A few things, right of the start in main(). You never check if you successfully open the file, then you attempt to use fscanf to read in strings in two variables (first, last) declared char.

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by Subsonics View Post
    A few things, right of the start in main(). You never check if you successfully open the file, then you attempt to use fscanf to read in strings in two variables (first, last) declared char.
    Which just proves my point:

    "Compiles" does not mean "works" or "tested".

    You didn't even make sure you could read your names in properly, and then you went on to write another 250 lines of code!


    Quzah.
    Hope is the first step on the road to disappointment.

  9. #9
    Registered User
    Join Date
    Sep 2011
    Posts
    111
    Quote Originally Posted by wo0dy View Post
    i was compiling the whole time and didnt get the error until i finished the program. Iv tried debugging but cant seem to find where it is crashing. What is the best way to debug?
    What they are talking about is, when you write a function, debug and test to make sure the function does what you want it to. Then move to the next function, do the same, and so on.

    For debugging, it was suggested to us in my class to use GDB.

    GDB, pretty much compile with the -g flag and you can run GDB.

    Another tool we use also is Valgrind (great for spotting mem leaks). The trick with Valgrind is fix the first problem at the top, and run it again, and work down.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 01-13-2010, 11:16 PM
  2. access violation segmentation fault raised in your program
    By dinamit875 in forum C++ Programming
    Replies: 8
    Last Post: 04-23-2009, 11:44 AM
  3. Getting Access Violation (Segmentation Fault)
    By Brownie in forum C++ Programming
    Replies: 2
    Last Post: 09-26-2008, 11:43 AM
  4. Replies: 3
    Last Post: 07-17-2008, 08:45 AM
  5. access violation (segmentation fault)
    By MarlonDean in forum C++ Programming
    Replies: 7
    Last Post: 05-16-2008, 05:02 AM

Tags for this Thread