Seg Faults

Discussion in 'C Programming' started by Matt, Aug 6, 2007.

  1. Matt

    Matt Guest

    Hey guys. I've got a general question here regarding segmentation
    faults.

    I've got some code that I started working on a few weeks ago which at
    the time ran without any problems. In the course of my coding I came
    across a segmentation fault and needed to check something in the
    original version of the code in hope of finding where I had gone
    wrong. However, the original code now seg faults as well! The fault
    ocurs in roughly the same place as before as the output from both
    versions is identical.

    I have tried loading up my original archive over and over but nothing
    seems to be working. I am certain the archive is sound as I have been
    using it as a reference point over the last few weeks without any
    problems.

    So, as the programme is far too large to post up, I was hoping to draw
    on the experience of you guys and come up with some possible sources
    of this error. Any suggestions are welcome.

    Kind Regards,

    Matt
    Matt, Aug 6, 2007
    #1
    1. Advertising

  2. Matt

    Richard Guest

    Matt <> writes:

    > Hey guys. I've got a general question here regarding segmentation
    > faults.
    >
    > I've got some code that I started working on a few weeks ago which at
    > the time ran without any problems. In the course of my coding I came
    > across a segmentation fault and needed to check something in the
    > original version of the code in hope of finding where I had gone
    > wrong. However, the original code now seg faults as well! The fault
    > ocurs in roughly the same place as before as the output from both
    > versions is identical.
    >
    > I have tried loading up my original archive over and over but nothing
    > seems to be working. I am certain the archive is sound as I have been
    > using it as a reference point over the last few weeks without any
    > problems.
    >
    > So, as the programme is far too large to post up, I was hoping to draw
    > on the experience of you guys and come up with some possible sources
    > of this error. Any suggestions are welcome.
    >
    > Kind Regards,
    >
    > Matt
    >


    Number 1 "doh" check : are you 100% sure you are executing your
    recompiled from archive version? Check your path.

    Number 2 "why didn't I think of that" is simply compile your archive
    code with a comment in it indicating "test day" or something and run it
    in a debugger .... when you run debugger check the "test day" comment is
    in your code and run it until it segfaults.
    Richard, Aug 6, 2007
    #2
    1. Advertising

  3. Matt

    Army1987 Guest

    On Mon, 06 Aug 2007 03:17:18 -0700, Matt wrote:

    > Hey guys. I've got a general question here regarding segmentation
    > faults.
    >
    > I've got some code that I started working on a few weeks ago which at
    > the time ran without any problems. In the course of my coding I came
    > across a segmentation fault and needed to check something in the
    > original version of the code in hope of finding where I had gone
    > wrong. However, the original code now seg faults as well! The fault
    > ocurs in roughly the same place as before as the output from both
    > versions is identical.
    >
    > I have tried loading up my original archive over and over but nothing
    > seems to be working. I am certain the archive is sound as I have been
    > using it as a reference point over the last few weeks without any
    > problems.
    >
    > So, as the programme is far too large to post up, I was hoping to draw
    > on the experience of you guys and come up with some possible sources
    > of this error. Any suggestions are welcome.


    Carefully check that you're never dereferencing a pointer which
    doesn't point to memory you own. (Note that doing so does not
    always causes a segfault, it may depend on many more conditions
    that you could imagine.)
    Try to isolate the part which causes the segfault, i.e. write the
    smallest possible program which exhibits the problem. This will
    usually show you where the problem is, if it doesn't post the
    reduced program here.

    --
    Army1987 (Replace "NOSPAM" with "email")
    "Never attribute to malice that which can be adequately explained
    by stupidity." -- R. J. Hanlon (?)
    Army1987, Aug 6, 2007
    #3
  4. On Aug 6, 6:17 am, Matt <> wrote:
    > Hey guys. I've got a general question here regarding segmentation
    > faults.
    >

    .....
    >
    > So, as the programme is far too large to post up, I was hoping to draw
    > on the experience of you guys and come up with some possible sources
    > of this error. Any suggestions are welcome.
    >


    Without code, people can only offer generalities, like checking
    pointers for NULL prior to dereferencing them, not using freed memory,
    not using uninitialized pointers, not overwriting bounds of allocated
    memory, etc. If you always crash at the same point in the program
    (seen by logging/etc), it is usually straightforward to figure out
    why. Add some print statements showing the objects in use (pointers,
    etc), and figure out the problem that way. If you crash at random
    points, that is trickier.

    <OT>If you are on a system where a core file is available after a
    segmentation fault, that can be helpful. Sometimes you need to enable
    this (e.g. via ulimit on linux).

    There are also lots of tools that can rescue you. If you always crash
    in the same place, running under a debugger, putting a breakpoint
    there, and looking at things can help. There are some tools, which
    may be available on your system, some free, some not, like valgrind,
    electric fence, purify, etc, that can be very nice for tracking down
    this sort of problem. If you use glibc, the MALLOC_CHECK_ environment
    variable set to 2 can occasionally help too, causes core dump on
    bounds overwrites, though only when the memory is actually freed.
    </OT>

    -David
    David Resnick, Aug 6, 2007
    #4
  5. Army1987 <> wrote:

    > On Mon, 06 Aug 2007 03:17:18 -0700, Matt wrote:


    > > Hey guys. I've got a general question here regarding segmentation
    > > faults.


    Finding them can be a painful exercise. If you're not able to spot
    your error by examining the code, you might find a program such as
    valgrind to be useful - check Google for more details.

    --
    C. Benson Manica | I appreciate all corrections, polite or otherwise.
    cbmanica(at)gmail.com |
    ----------------------| I do not currently read any posts posted through
    sdf.lonestar.org | Google groups, due to rampant unchecked spam.
    Christopher Benson-Manica, Aug 6, 2007
    #5
  6. Matt

    John Gordon Guest

    In <> Matt <> writes:

    > I've got some code that I started working on a few weeks ago which at
    > the time ran without any problems. In the course of my coding I came
    > across a segmentation fault and needed to check something in the
    > original version of the code in hope of finding where I had gone
    > wrong. However, the original code now seg faults as well! The fault
    > ocurs in roughly the same place as before as the output from both
    > versions is identical.


    One explanation is that the original code actually did have the problem,
    but due to sheer luck you just never saw it.

    --
    John Gordon A is for Amy, who fell down the stairs
    B is for Basil, assaulted by bears
    -- Edward Gorey, "The Gashlycrumb Tinies"
    John Gordon, Aug 6, 2007
    #6
  7. On Aug 6, 3:17 am, Matt <> wrote:
    > Hey guys. I've got a general question here regarding segmentation
    > faults.
    >
    > I've got some code that I started working on a few weeks ago which at
    > the time ran without any problems. In the course of my coding I came
    > across a segmentation fault and needed to check something in the
    > original version of the code in hope of finding where I had gone
    > wrong. However, the original code now seg faults as well! The fault
    > ocurs in roughly the same place as before as the output from both
    > versions is identical.
    >
    > I have tried loading up my original archive over and over but nothing
    > seems to be working. I am certain the archive is sound as I have been
    > using it as a reference point over the last few weeks without any
    > problems.
    >
    > So, as the programme is far too large to post up, I was hoping to draw
    > on the experience of you guys and come up with some possible sources
    > of this error. Any suggestions are welcome.
    >
    > Kind Regards,
    >
    > Matt



    Assuming that "both" version of your code fail then chances are:

    -You ran the code under different users, earlier one might have access
    to resources that later one does not?
    -Resources which were available earlier are gone now?

    In both cases you could have a failed open/mmap/ioctl and did not
    properly check return value. Then using those bad handle/pointer could
    lead to segfault.
    phuong.d.nguyen, Aug 7, 2007
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Andrew King
    Replies:
    1
    Views:
    294
    John Harrison
    Apr 7, 2004
  2. Sharad Kala

    Re: malloc creates seg faults?

    Sharad Kala, Feb 14, 2005, in forum: C++
    Replies:
    2
    Views:
    296
    Sharad Kala
    Feb 14, 2005
  3. Berk Birand

    malloc creates seg faults?

    Berk Birand, Feb 14, 2005, in forum: C++
    Replies:
    7
    Views:
    365
    Artie Gold
    Feb 14, 2005
  4. Replies:
    4
    Views:
    397
    Ioannis Vranos
    Feb 15, 2005
  5. Jane Austine
    Replies:
    0
    Views:
    308
    Jane Austine
    Aug 14, 2003
Loading...

Share This Page