Segmentation fault with input test file

This is a discussion on Segmentation fault with input test file within the C Programming forums, part of the General Programming Boards category; Hi, ive got a program that runs fine except when I run an input test file to compare differences with ...

  1. #1
    Registered User
    Join Date
    Apr 2002
    Posts
    13

    Segmentation fault with input test file

    Hi, ive got a program that runs fine except when I run an input test file to compare differences with an output test file. It then gives me a segmentation fault. What are some of the causes of this?

    ie.

    a.out <input3> tmp
    Segmentation fault (core dumped)

  2. #2
    Mayor of Awesometown Govtcheez's Avatar
    Join Date
    Aug 2001
    Location
    MI
    Posts
    8,825
    ARe you trying to access memory that doesn't belong to you? Post some code.

  3. #3
    Comment your source code! Lynux-Penguin's Avatar
    Join Date
    Apr 2002
    Posts
    533
    if you really have those < and >'s in there then of course it's going to fault
    your redirecting input and output from each other and I doubt your program has something meant to handle REDIR

    Post some code, then we can help you better
    Asking the right question is sometimes more important than knowing the answer.
    Please read the FAQ
    C Reference Card (A MUST!)
    Pointers and Memory
    The Essentials
    CString lib

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,558
    > if you really have those < and >'s in there then of course it's going to fault
    Nothing wrong with the command - looks like a standard way for a program to read from one file, and write to another

    > and I doubt your program has something meant to handle REDIR
    Redirection is handled by the shell, not the program
    All the program still sees is stdin and stdout

    But for sure, bentles has some bugs, and should post some code.

  5. #5
    Registered User
    Join Date
    Apr 2002
    Posts
    13
    there are 4 files: a header file, a list file, a main file and a another file doing the operations:
    Attached Files Attached Files

  6. #6
    Registered User
    Join Date
    Apr 2002
    Posts
    13
    main.c....
    Attached Files Attached Files
    • File Type: c main.c (882 Bytes, 66 views)

  7. #7
    Registered User
    Join Date
    Apr 2002
    Posts
    13
    list.c...
    Attached Files Attached Files

  8. #8
    Registered User
    Join Date
    Apr 2002
    Posts
    13
    I know the program looks dodgy, but it works...well... mostly

  9. #9
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Haven't looked at ya code yet, but have you tried putting some printf's at key points within the code? This'll at least give you a clue about where it's going wrong.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  10. #10
    Registered User
    Join Date
    Apr 2002
    Posts
    13
    Originally posted by Hammer
    Haven't looked at ya code yet, but have you tried putting some printf's at key points within the code? This'll at least give you a clue about where it's going wrong.
    The code works fine and there doesnt seem to be any problems whatsoever, but when using an input test file, it gives a segmentation fault. There are two input test files, one works, but i cant get the other working.

  11. #11
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Would still suggest you put from printf's in there to try and determine which function is causing the failure. Just add something like this to the start of each of function.

    Code:
    fprintf(stderr, "Starting function <insertfunctionnamehere>\n");
    Have had a quick look through the code. I see you do some scanf's and strcpy's. Their bounds checking is dogdy, are you sure you test data doesn't exceed the array bounds?

    Be careful here too:

    Code:
    while(strcmp(units[low]->name, unit_name) !=0)
    {
           low++;
    }
    The unit_name variable is read from an external source and is therefore unrealiable. If the unit_name does not exist within your array (remember it's a case sensative search too), the low variable will eventually exceed the bounds for the units variable, and you'll crash&burn.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  12. #12
    Registered User
    Join Date
    Apr 2002
    Posts
    13
    Originally posted by Hammer
    Would still suggest you put from printf's in there to try and determine which function is causing the failure. Just add something like this to the start of each of function.

    Code:
    fprintf(stderr, "Starting function <insertfunctionnamehere>\n");
    Have had a quick look through the code. I see you do some scanf's and strcpy's. Their bounds checking is dogdy, are you sure you test data doesn't exceed the array bounds?

    Be careful here too:

    Code:
    while(strcmp(units[low]->name, unit_name) !=0)
    {
           low++;
    }
    The unit_name variable is read from an external source and is therefore unrealiable. If the unit_name does not exist within your array (remember it's a case sensative search too), the low variable will eventually exceed the bounds for the units variable, and you'll crash&burn.


    This is the test data so i cant see how it could be the size of the unit_name array thats the problem:

    1
    a
    1
    b
    1
    c
    6
    a
    4
    999999 a
    4
    2 a41 a
    6
    a
    5
    1 a
    5
    2 a
    6
    a
    0

    Ive inputted all this data outside the file and it works ok....one thing ive noticed is that if i do this..copy and paste all the file info....it usually prints the output from an operation a few lines down. ie. for the operation 6 that prints out all units in a, it usually prints the empty line after the operation 4 which adds 999999 to a. I dont know if this is important....

  13. #13
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    can you post the header file.....
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  14. #14
    Registered User
    Join Date
    Apr 2002
    Posts
    13
    here
    Attached Files Attached Files
    • File Type: h list.h (931 Bytes, 84 views)

  15. #15
    Registered User
    Join Date
    Apr 2002
    Posts
    13
    another thing is that the file works if i remove the last one of the '4' operations, which adds a student, which suggests the problem lies in the add_student function.

Page 1 of 2 12 LastLast
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, 09:46 AM
  4. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM

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