Thread: Need help fixing segmentation fault

  1. #46
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by Avenger625 View Post
    I hate being rude.....but...........Are u really a nut??? When did i say or imply in anyway that error checking(s) are useless?! And, I'm unlike u, trying to prove someone wrong or prove myself superior than someone!! I have no such intentions. I'm here to discuss and enhance my knowledge and learn more. And in doing so, if can add value to the forum.
    Check the following......
    If you want to know where you said that error checking was useless, that was post #40. Although to be honest, I'm not sure anymore what point you're making.

    The idea is that the input format, as given in the original post, is " %c %c %d". You can use a different format if you want, but then you are going to be matching a different format than you are supposed to be using, so either you will (1) fail to match proper input or (2) improperly match incorrect input or (3) both, depending on how you muck it up. In this case leaving out the second space will cause (2) to happen, and CT conveniently posted an improper input file that your code will claim is accurate. [It will eventually fail on the next line, so not too damage is done, but.]

    EDIT EDIT EDIT: And you know, by now, you'd think I'd remember how spaces in format strings actually work, but I didn't: a space in a format string can match no spaces in the input, so the original code would fail in the same way. So all of this fuss for nothing! My apologies. (We'd have to read a line with fgets or similar and check there's nothing left over to actually check.)
    Last edited by tabstop; 09-11-2011 at 07:42 PM.

  2. #47
    Registered User
    Join Date
    Jan 2010
    Posts
    412
    Quote Originally Posted by Avenger625 View Post
    I hate being rude.....but...........Are u really a nut??? When did i say or imply in anyway that error checking(s) are useless?! And, I'm unlike u, trying to prove someone wrong or prove myself superior than someone!! I have no such intentions. I'm here to discuss and enhance my knowledge and learn more. And in doing so, if can add value to the forum.
    Check the following......
    Quite possible that I am And maybe 'useless' was a bit too harsh. I'm sorry about that.
    But looking at your format string it kind of implies that proper validation is unneeded here.

    Going by the example input in post #1 we can assume that the input format should be
    character space character space digit newline
    implying* that everything else is invalid input.
    Your format string will accept "w A1 4" as 'w', 'A', 1 when it should be classified as invalid.
    The OP didn't specify if the program should skip and continue, or abort on invalid input so I guess either way is fine.
    And ideally fscanf should be writing to temporary variables and then only update the arrays if the whole line is valid.

    *) Yes, assumptions are generally bad. But since we only have an example and no actual input specs it's all we have to go on.

    Edit: Woah, browser cache issues. tabstop's post didn't show up until after I posted this o_O
    Quote Originally Posted by tabstop
    And you know, by now, you'd think I'd remember how spaces in format strings actually work, but I didn't: a space in a format string can match no spaces in the input, so the original code would fail in the same way. So all of this fuss for nothing! My apologies. (We'd have to read a line with fgets or similar and check there's nothing left over to actually check.)
    Ouch, good catch. I just realized I made the same mistake.
    I guess you could use something like "%*[ ]" to validate that there's a space character there.
    Last edited by _Mike; 09-11-2011 at 07:56 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Segmentation Fault
    By tlman12 in forum C Programming
    Replies: 1
    Last Post: 10-29-2010, 03:57 PM
  2. Segmentation fault
    By Miffeltoffel in forum C Programming
    Replies: 8
    Last Post: 10-21-2010, 03:55 AM
  3. Re: Segmentation fault
    By turkish_van in forum C Programming
    Replies: 8
    Last Post: 01-20-2007, 05:50 PM
  4. Segmentation Fault
    By Haos in forum C Programming
    Replies: 2
    Last Post: 09-18-2004, 08:00 PM
  5. segmentation fault and memory fault
    By Unregistered in forum C Programming
    Replies: 12
    Last Post: 04-02-2002, 11:09 PM