Thread: Strange core dump

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    20

    Question Strange core dump

    I have a program that compiled on my i386 running suse linux with

    gcc -Wall student.c and runs perfectly.

    When trying to compile and run the same program on a unix machine, I am recieving a segmentation fault at the following code

    Code:
        fgets(line, sizeof(line), fp);
        if(!sscanf(line, "%d", &numRecords)) {
            printf("File not in the correct format.\n");
            exit(1);
        }
    If i remove the unary & operator, there is no segmentation fault, but of couse numRecords is garbage and unusable. I don't understand how this works on one system and not another. Any suggestions?

    entire source attached.

  2. #2
    Registered User
    Join Date
    Sep 2005
    Posts
    20
    INPUT FILE
    ---------------------------
    Code:
    5
    Jeffrey Carouth 371625342 MATH152 4 3 PHYS218 4 4 ENGR111 2 4 HIST105 3 3 CPSC206 4 4
    Bob Dillan 873193526 MATH151 4 3 PHYS218 4 4 ENGR111 2 3 KINE199 1 4 POLS206 3 4
    Matthew Bennett 943721745 MATH150 4 2 ENGR111 2 2 PHYS218 4 1 HIST105 3 3 LAND111 3 1
    Jasen Petersen 624392761 MATH111 3 4 BUSI112 4 3 MGMT209 4 4 HIST109 3 3 MKTG209 4 4
    Jordan Brown 926873321 ENGL432 3 4 ENGR482 3 4 CPSC469 3 3 MKTG422 4 4 HIST322 3 4

  3. #3
    Registered User
    Join Date
    Apr 2005
    Posts
    134
    You need to check for NULL with fgets() which is returned at EOF.

    Code:
    if (fgets(line, sizeof(line), fp) != NULL)
    {
        if(!sscanf(line, "%d", &numRecords)) {
    	printf("File not in the correct format.\n");
    	exit(1);
        }
    }

  4. #4
    Registered User
    Join Date
    Sep 2005
    Posts
    20
    Doing a

    Code:
    fgets(line, sizeof(line), fp);
    puts(line);
    //sscanf code
    I get an output string of
    5
    as expected.

  5. #5
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    The original code doesn't like empty lines in the input-file.
    Kurt

  6. #6
    Registered User
    Join Date
    Sep 2005
    Posts
    20
    That was the idea.....

    There are no empty lines in the input file.

  7. #7
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    If I run your original code with an inputfile without empty lines it runs fine.
    If I put an empty line as first line into the file it seg-faults.
    Kurt

  8. #8
    Registered User
    Join Date
    Apr 2005
    Posts
    134
    I have modified the original code. I could complie it on Windows as well as Unix platform.

  9. #9
    Registered User
    Join Date
    Sep 2005
    Posts
    20
    Thank you nkhambal. It compiled and ran on the machine in question. Can I ask you to explain the changes you made?

  10. #10
    Registered User
    Join Date
    Apr 2005
    Posts
    134
    Here is the diff between your code and the one that I modified.

    studentold.c is your code and student.c is mine. "<" indicates code lines removed/changed from your code and ">" indicates the code lines added by me.

    Thanks,

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Strange core dump, help please
    By Smattacus in forum C Programming
    Replies: 6
    Last Post: 09-30-2008, 11:50 AM
  2. having a core dump
    By justins in forum C Programming
    Replies: 6
    Last Post: 05-21-2008, 12:00 PM
  3. STL vector core dump
    By creativeinspira in forum C++ Programming
    Replies: 9
    Last Post: 07-22-2007, 04:49 PM
  4. Core Dump in While()
    By KrepNatas in forum C Programming
    Replies: 5
    Last Post: 05-17-2005, 11:15 AM
  5. core dump
    By kermit in forum Linux Programming
    Replies: 0
    Last Post: 08-03-2004, 06:25 PM