Segmentation Fault on Linux, but not on Solaris

Discussion in 'C Programming' started by John, Nov 7, 2003.

  1. John

    John Guest

    I'm getting a segmentation fault when I try to realloc memory.

    This only happens when I compile using gcc v. 3.2 on Red Hat Linux 8.0
    3.2-7. When I compile on Solaris 5.7 using gcc v. 2.95.2, the error
    does not occur.

    Is there a way to solve this using compiler options, or any other
    method? In the end, I need this program to compile on the Linux box.

    Any help would be greatly appreciated!!!
    Thanks.
    John, Nov 7, 2003
    #1
    1. Advertising

  2. John

    Eric Sosman Guest

    John wrote:
    >
    > I'm getting a segmentation fault when I try to realloc memory.
    > [...]


    This is Question 7.19 in the comp.lang.c Frequently
    Asked Questions (FAQ) list

    http://www.eskimo.com/~scs/C-faq/top.html

    --
    Eric Sosman, Nov 7, 2003
    #2
    1. Advertising

  3. John

    Artie Gold Guest

    John wrote:
    > I'm getting a segmentation fault when I try to realloc memory.
    >
    > This only happens when I compile using gcc v. 3.2 on Red Hat Linux 8.0
    > 3.2-7. When I compile on Solaris 5.7 using gcc v. 2.95.2, the error
    > does not occur.
    >
    > Is there a way to solve this using compiler options, or any other
    > method? In the end, I need this program to compile on the Linux box.
    >

    Most likely, you've corrupted your free store (often known as the
    `heap'), which causes undefined behavior. Since `seems to work' is
    one of the possible outcomes of UB...

    IOW, there's a bug in your program (most likely a buffer overrun,
    but I'm guessing). The difference in behavior you're seeing, BTW,
    most likely has more to do with the underlying implementation of the
    standard library on the two platforms as opposed to the compiler used.

    HTH,
    --ag

    --
    Artie Gold -- Austin, Texas
    Oh, for the good old days of regular old SPAM.
    Artie Gold, Nov 7, 2003
    #3
  4. John wrote:
    >
    > I'm getting a segmentation fault when I try to realloc memory.
    >
    > This only happens when I compile using gcc v. 3.2 on Red Hat Linux 8.0
    > 3.2-7. When I compile on Solaris 5.7 using gcc v. 2.95.2, the error
    > does not occur.
    >
    > Is there a way to solve this using compiler options, or any other
    > method? In the end, I need this program to compile on the Linux box.
    >
    > Any help would be greatly appreciated!!!


    Since the problem is on Linux, you can try running it
    with valgrind (search http://freshmeat.net/ ) which is
    really good at finding these kinds of errors. Unfortunately
    it only runs ix86 Linux.

    Erik
    --
    +-----------------------------------------------------------+
    Erik de Castro Lopo (Yes it's valid)
    +-----------------------------------------------------------+
    IDIOT, n.
    A member of a large and powerful tribe whose influence in human
    affairs has always been dominant and controlling. The Idiot's
    activity is not confined to any special field of thought or action,
    but "pervades and regulates the whole." He has the last word in
    everything; his decision is unappealable. He sets the fashions and
    opinion of taste, dictates the limitations of speech and
    circumscribes conduct with a dead-line.
    Ambrose Bierce
    Erik de Castro Lopo, Nov 7, 2003
    #4
  5. Artie Gold wrote:
    > John wrote:
    >
    >> I'm getting a segmentation fault when I try to realloc memory.
    >>
    >> This only happens when I compile using gcc v. 3.2 on Red Hat Linux 8.0
    >> 3.2-7. When I compile on Solaris 5.7 using gcc v. 2.95.2, the error
    >> does not occur.
    >>
    >> Is there a way to solve this using compiler options, or any other
    >> method? In the end, I need this program to compile on the Linux box.
    >>

    > Most likely, you've corrupted your free store (often known as the
    > `heap'), which causes undefined behavior.


    Agreed. I am author of a cross-platform C program. In the "early days"
    I used to think (e.g.) "It works fine on Linux but 'performs an
    illegal operation' on Windows, what the heck is wrong with Windows?"
    Well, OK, there are plenty of things wrong with Windows, but my
    point is that, as I have learned, with very high probability it's
    a bug in your/my code that "happens to" show itself as a segfault on
    one platform even though it seems to "work fine" on another.

    Actually, the overt appearance of a segfault is good! It forces
    you to track down the bug -- e.g. with valgrind

    http://developer.kde.org/~sewardj/


    --
    Allin Cottrell
    Department of Economics
    Wake Forest University, NC
    Allin Cottrell, Nov 8, 2003
    #5
  6. "Allin Cottrell" <> wrote in message
    news:bohil1$340o$...

    (snip)

    > Agreed. I am author of a cross-platform C program. In the "early days"
    > I used to think (e.g.) "It works fine on Linux but 'performs an
    > illegal operation' on Windows, what the heck is wrong with Windows?"
    > Well, OK, there are plenty of things wrong with Windows, but my
    > point is that, as I have learned, with very high probability it's
    > a bug in your/my code that "happens to" show itself as a segfault on
    > one platform even though it seems to "work fine" on another.


    I once had a program with an (accidental) uninitialized variable that worked
    on one system and not on another. It turned out that my program depended on
    it NOT being initialized to zero, and one system did initialize it to zero.

    -- glen
    Glen Herrmannsfeldt, Nov 8, 2003
    #6
  7. John

    Mac Guest

    On Fri, 07 Nov 2003 12:36:43 +0000, John wrote:

    > I'm getting a segmentation fault when I try to realloc memory.
    >
    > This only happens when I compile using gcc v. 3.2 on Red Hat Linux 8.0
    > 3.2-7. When I compile on Solaris 5.7 using gcc v. 2.95.2, the error
    > does not occur.
    >
    > Is there a way to solve this using compiler options, or any other
    > method? In the end, I need this program to compile on the Linux box.
    >
    > Any help would be greatly appreciated!!!
    > Thanks.


    The way your question is written, you almost make it sound like the
    compiler is segfaulting.

    But I gather that it is your compiled executable, not the compiler. The
    most likely explanation is that you have a bug in your code. The only
    compiler flags which are likely to help are the ones which perform checks
    on your code to warn against possible bugs. (e.g., -Wall -pedantic)

    If you can simplify the code to the smallest example which still exhibits
    the segfault, making sure in the process to remove any platform dependent
    stuff which would make it fail to compile on other platforms, then you can
    post the code here and I am sure someone will find the problem.

    If you do this, make sure you cut and paste the program, otherwise all the
    feedback you get will probably be over a typo. ;-)

    Mac
    --
    Mac, Nov 8, 2003
    #7
  8. John

    CBFalconer Guest

    John wrote:
    >
    > I'm getting a segmentation fault when I try to realloc memory.
    >
    > This only happens when I compile using gcc v. 3.2 on Red Hat Linux
    > 8.0 3.2-7. When I compile on Solaris 5.7 using gcc v. 2.95.2, the
    > error does not occur.
    >
    > Is there a way to solve this using compiler options, or any other
    > method? In the end, I need this program to compile on the Linux box.
    >
    > Any help would be greatly appreciated!!!


    You probably have an error in line 242. At least that is the best
    we can do with the prodigious source code you have supplied. I.E.
    cut the problem down to a compilable source of about 100 lines or
    less which demonstrates the problem, and post that here. Paste
    compilable source, not your approximation of such. Then possibly
    someone may be able to point out the error.

    --
    Chuck F () ()
    Available for consulting/temporary embedded and systems.
    <http://cbfalconer.home.att.net> USE worldnet address!
    CBFalconer, Nov 8, 2003
    #8
  9. John

    Don Porges Guest

    "Erik de Castro Lopo" <> wrote in message news:...
    > John wrote:
    > >
    > > I'm getting a segmentation fault when I try to realloc memory.
    > >
    > > This only happens when I compile using gcc v. 3.2 on Red Hat Linux 8.0
    > > 3.2-7. When I compile on Solaris 5.7 using gcc v. 2.95.2, the error
    > > does not occur.
    > >
    > > Is there a way to solve this using compiler options, or any other
    > > method? In the end, I need this program to compile on the Linux box.
    > >
    > > Any help would be greatly appreciated!!!

    >
    > Since the problem is on Linux, you can try running it
    > with valgrind (search http://freshmeat.net/ ) which is
    > really good at finding these kinds of errors. Unfortunately
    > it only runs ix86 Linux.


    Let me extend your suggestion: *whether or not the problem is
    seen on Linux*, you can try valgrind and see what it says. Remember, if the problem
    is an uninitialized variable, or a bad use of malloc or free, valgrind's analysis will find it
    even if it doesn't cause a crash in that environment.
    Don Porges, Nov 8, 2003
    #9
  10. John

    Dan Pop Guest

    In <> (John) writes:

    >I'm getting a segmentation fault when I try to realloc memory.
    >
    >This only happens when I compile using gcc v. 3.2 on Red Hat Linux 8.0
    >3.2-7. When I compile on Solaris 5.7 using gcc v. 2.95.2, the error
    >does not occur.
    >
    >Is there a way to solve this using compiler options, or any other
    >method? In the end, I need this program to compile on the Linux box.


    Sure. Use the -fix-my-bugs compiler option.

    Dan
    --
    Dan Pop
    DESY Zeuthen, RZ group
    Email:
    Dan Pop, Nov 10, 2003
    #10
    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. Alex Hunsley
    Replies:
    17
    Views:
    863
  2. Replies:
    0
    Views:
    526
  3. Gregor Rot
    Replies:
    5
    Views:
    6,774
    Chris Dams
    Jan 7, 2004
  4. Replies:
    4
    Views:
    1,961
  5. Replies:
    2
    Views:
    525
    Roedy Green
    May 15, 2012
Loading...

Share This Page