Thread: Seg fault

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    37

    Seg fault

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    #define TWOI 2
    #define CLASS_SIZE 20
    #define NAME_SIZE 20
    #define ONEI 1
    #define SALARY_SIZE 2
    #define TEXT_BUFFER 81
    #define ZEROI 0
    
    struct employee
    {
      char *firstName;
      char *lastName;
      int id;
      char *class;
      double salary[SALARY_SIZE];
      struct employee *nextEmployee;
    };
    
    void hire(FILE **, char [], struct employee **);
    void printList(struct employee *);
    Thats my constants file
    Code:
    #include "hw3AndyFoxConstants.c"
    
    int main()
    {
     printf("hello");
     struct employee *firstEmployee = NULL;
     FILE *infile;
     char lineOfText[NAME_SIZE] = {""};
     
     infile  = fopen("hw3AndyFoxInput.dat", "r");
     fgets(lineOfText, NAME_SIZE, infile);
     while(!feof(infile))
     {
       if(lineOfText[ZEROI] == '*')
       {
         switch(lineOfText[TWOI])
         {
          case 'H':
    	    hire(&infile, lineOfText, &firstEmployee);
    	    printList(firstEmployee);
            break;
          case 'I':
           // initial();
            break;
          case 'P':
            //promote();
    	    break;
    	  case 'T':
            //transfer();
            break;
    	  case 'E':
    	    //endOfData();
            break;
          default:
            printf("Invalid input\n");
     	    break;
          }
        }
      }
    return(ZEROI);
    }
    This is part of my main program.
    I keep getting a seg fault over and over, the program wont even print the "hello" statement I put at the top of the main function. I have figured that if i comment out all the commands having to do with the input file it will print the hello. The program is working with a singularly linked list.

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    1. check the return value of any function that can fail - like fopen before proceeding
    2. while(!feof(infile)) - do not use feof in loops - read FAQ http://faq.cprogramming.com/cgi-bin/...&id=1043284351
    3. you need your reading inside the loop
    and where is your hire function?
    in what line the crash occured?
    have you tried to debug your problem?
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    Registered User
    Join Date
    Oct 2007
    Posts
    37
    I've been trying many things for the past day, and from what I can tell is that it is not even reaching the main function. The printf("hello"); is not printing, which is the first thing in the main function. In XCODE on my mac, I get a "exited due to signal 10 (SIGBUS). On my schools linux server, I can compile the program with gcc but when I type a.out to run it nothing happens I just get a blank line and no command prompt and it stays this way until I press cntrl c.

  4. #4
    Registered User
    Join Date
    Oct 2007
    Posts
    37
    Ah, I tried the method in your link, my file is not opening correctly. Now I'm just not sure where to put my input file in xcode.

  5. #5
    Nub SWE
    Join Date
    Mar 2008
    Location
    Dallas, TX
    Posts
    133
    Code:
    infile  = fopen("c:\\hw3AndyFoxInput.dat", "r");
    Then just put it on your C drive.

  6. #6
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by Fox101 View Post
    I've been trying many things for the past day, and from what I can tell is that it is not even reaching the main function. The printf("hello"); is not printing, which is the first thing in the main function.
    That is probably only happening because you forgot to put '\n' at the end of the string. The program crashed before it could flush the output buffer, so you never saw the message. It doesn't mean it's crashing before main().

  7. #7
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Also, what exactly is the purpose of the ZEROI, ONEI, and TWOI macros? What is the point? Were you planning on changing the value of two?

  8. #8
    Registered User
    Join Date
    Oct 2007
    Posts
    37
    We are not allowed to use hard coded numbers. Our prof wants every number to be defined in the constants file.

    *The program now runs through main now on the unix server, just cant figure out where to put the input file on xcode.*

  9. #9
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by Fox101 View Post
    *The program now runs through main now on the unix server, just cant figure out where to put the input file on xcode.*
    But: you're specifiying no path whatsoever when you open the file, therefore your input file has to be right where you are. If there's a "project path" or something in XCode, go there; otherwise it's in the same directory as everything else.

  10. #10
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by Fox101 View Post
    We are not allowed to use hard coded numbers. Our prof wants every number to be defined in the constants file.
    You're missing the point. Why is it called "TWOI"? The purpose of defining such constants is so you can easily change their values in the future. So imagine you change this constant to 3. Now you have:

    Code:
    #define TWOI 3
    So the constant says "TWO" yet its value is 3. I hope you can see why that's silly. The problem is the name. The name should indicate what the value is for, not what its specific value is. Otherwise it defeats the point of even doing this.

    EDIT: Also, the numbers "0" and "1" occur very commonly. There is nothing wrong with literally saying "0" or "1" when you need it, despite what your professor says.

  11. #11
    Registered User
    Join Date
    Oct 2007
    Posts
    37
    Yeh, I know what you mean, however he subtracts points if I use 0 or 1 heh.

  12. #12
    Registered User
    Join Date
    Oct 2007
    Posts
    37
    Quote Originally Posted by tabstop View Post
    But: you're specifiying no path whatsoever when you open the file, therefore your input file has to be right where you are. If there's a "project path" or something in XCode, go there; otherwise it's in the same directory as everything else.
    Its in the scource area with my constants and main.c file.

  13. #13
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Is that where your executable is? If so, you're probably golden; and if you're not, try running it on the command line.

  14. #14
    Nub SWE
    Join Date
    Mar 2008
    Location
    Dallas, TX
    Posts
    133
    Quote Originally Posted by Fox101 View Post
    Yeh, I know what you mean, however he subtracts points if I use 0 or 1 heh.

    I think what brewbuck is saying is that you should give the #define a more meaningful name.

    What do you use 2 for? For example, if it was the maximum number of apples capable of fitting in a barrel or something, I would call it MAX_APPLES, not TWOI.

  15. #15
    Registered User
    Join Date
    Oct 2007
    Posts
    37
    Ah ok.

    *What exactly does the perror() function do. Right now it is printing out success after my file name, but using the example given in the link above, the return(EXIT_FAILURE) is run emmediatly after. I know though that this if statement would not run if the pointer the file was not null, so I'm guessing my file is still failing to open. *
    Last edited by Fox101; 04-08-2008 at 02:24 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting a seg fault
    By ammochck21 in forum C Programming
    Replies: 11
    Last Post: 01-23-2009, 05:27 AM
  2. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  3. seg fault at vectornew
    By tytelizgal in forum C Programming
    Replies: 2
    Last Post: 10-25-2008, 01:22 PM
  4. weird seg fault
    By Vermelho in forum C Programming
    Replies: 3
    Last Post: 05-10-2008, 08:27 PM
  5. Seg Fault Problem
    By ChazWest in forum C++ Programming
    Replies: 2
    Last Post: 04-18-2002, 03:24 PM