Segmentation fault

Discussion in 'C Programming' started by Allan, Oct 7, 2003.

  1. Allan

    Allan Guest

    Hi all,

    I've a C program which was originally developed under Sun
    Solaris(OS,2.6,gcc 2.95).

    I'm trying to migrate it to our linux system(Mandrake 9.0,2.4.19,gcc
    3.2).

    Following is the MAKEFILE:
    CC=gcc
    CFLAGS= -march=athlon-mp -O3 -pipe -fomit-frame-pointer
    CHOST= -i686-pc-linux-gnu
    LIBS=$(SYSLIB) -lm
    OBJS=filename.o

    # Rules to create .o files from .c files
    ..c.o:
    $(CC) -c $(CFLAGS) $(INCLUDES) $*.c

    all: filename

    filename: $(OBJS)
    $(CC) $(OBJS) $(CFLAGS) $(LIBS) $(CHOST) -o $@


    The program can be run very well under Solaris.
    Under Linux,there is no error when I compile it.But I got
    "Segmentation fault" when execute the program.

    Any help will be appreciated,

    TIA,
    Allan, Oct 7, 2003
    #1
    1. Advertising

  2. Allan

    Artie Gold Guest

    Allan wrote:
    > Hi all,
    >
    > I've a C program which was originally developed under Sun
    > Solaris(OS,2.6,gcc 2.95).
    >
    > I'm trying to migrate it to our linux system(Mandrake 9.0,2.4.19,gcc
    > 3.2).
    >
    > Following is the MAKEFILE:
    > CC=gcc
    > CFLAGS= -march=athlon-mp -O3 -pipe -fomit-frame-pointer
    > CHOST= -i686-pc-linux-gnu
    > LIBS=$(SYSLIB) -lm
    > OBJS=filename.o
    >
    > # Rules to create .o files from .c files
    > .c.o:
    > $(CC) -c $(CFLAGS) $(INCLUDES) $*.c
    >
    > all: filename
    >
    > filename: $(OBJS)
    > $(CC) $(OBJS) $(CFLAGS) $(LIBS) $(CHOST) -o $@
    >
    >
    > The program can be run very well under Solaris.
    > Under Linux,there is no error when I compile it.But I got
    > "Segmentation fault" when execute the program.
    >
    > Any help will be appreciated,
    >



    Obviously, the problem is on line #42.

    Seriously though -- just how do you expect a worthwhile answer from
    people who have not seen the code in question?

    Short answer:
    You have a bug which has invoked undefined behavior. On one OS, the
    u.b. meant it *seemed* to work; on the other it crashed.

    With more information, perhaps a better answer can be provided.

    HTH,
    --ag

    --
    Artie Gold -- Austin, Texas
    Oh, for the good old days of regular old SPAM.
    Artie Gold, Oct 7, 2003
    #2
    1. Advertising

  3. Allan

    Malcolm Guest

    "Allan" <> wrote in message
    >
    > The program can be run very well under Solaris.
    > Under Linux,there is no error when I compile it.But I got
    > "Segmentation fault" when execute the program.
    >

    A segmentation fault occurs when a program tries to access memory that it
    doesn't own. Unfortunately this is very common with C programs, since
    pointers can be invalid or arrays can be exceeded without the compiler
    noticing.
    You need to use a debugger to find out where the problem occurred. The error
    is likely to be within a few lines of the illegal access occurring, however
    this isn't guaranteed.
    Malcolm, Oct 7, 2003
    #3
  4. Allan

    CBFalconer Guest

    Allan wrote:
    >
    > I've a C program which was originally developed under Sun
    > Solaris(OS,2.6,gcc 2.95).
    >
    > I'm trying to migrate it to our linux system(Mandrake 9.0,2.4.19,
    > gcc 3.2).
    >
    > Following is the MAKEFILE:
    > CC=gcc
    > CFLAGS= -march=athlon-mp -O3 -pipe -fomit-frame-pointer
    > CHOST= -i686-pc-linux-gnu
    > LIBS=$(SYSLIB) -lm
    > OBJS=filename.o
    >
    > # Rules to create .o files from .c files
    > .c.o:
    > $(CC) -c $(CFLAGS) $(INCLUDES) $*.c
    >
    > all: filename
    >
    > filename: $(OBJS)
    > $(CC) $(OBJS) $(CFLAGS) $(LIBS) $(CHOST) -o $@
    >
    > The program can be run very well under Solaris.
    > Under Linux,there is no error when I compile it.But I got
    > "Segmentation fault" when execute the program.


    All the above facts are irrelevant and Off-Topic here. My crystal
    ball is murky, but it claims you generated a wild pointer in line
    42 of the 3rd .c module.

    --
    Chuck F () ()
    Available for consulting/temporary embedded and systems.
    <http://cbfalconer.home.att.net> USE worldnet address!
    CBFalconer, Oct 7, 2003
    #4
  5. Allan

    fermath Guest

    Malcolm wrote:

    >
    > "Allan" <> wrote in message
    >>
    >> The program can be run very well under Solaris.
    >> Under Linux,there is no error when I compile it.But I got
    >> "Segmentation fault" when execute the program.
    >>

    > A segmentation fault occurs when a program tries to access memory that it
    > doesn't own. Unfortunately this is very common with C programs, since
    > pointers can be invalid or arrays can be exceeded without the compiler
    > noticing.
    > You need to use a debugger to find out where the problem occurred. The
    > error is likely to be within a few lines of the illegal access occurring,
    > however this isn't guaranteed.


    If you're not able to find the bug with a debugger (please, use gdb) I
    recommend you to use a program called valgrind. Its purpose is to identify
    the sort of things Malcolm has explained. (out of range memory accesses,
    ecc.) ... It has saved my life ... sometimes

    Regards
    fermath, Oct 8, 2003
    #5
  6. On Tue, 7 Oct 2003, Allan wrote:

    > Hi all,
    >
    > I've a C program which was originally developed under Sun
    > Solaris(OS,2.6,gcc 2.95).
    >
    > I'm trying to migrate it to our linux system(Mandrake 9.0,2.4.19,gcc
    > 3.2).
    >
    > Following is the MAKEFILE:


    [SNIP]

    If the program builds and runs (which it must if you are getting a
    segmentation fault) there is most likely nothing wrong with the make file.
    Additionally, make files are not part of C language and therefore off
    topic for this newsgroup.

    > The program can be run very well under Solaris.
    > Under Linux,there is no error when I compile it.But I got
    > "Segmentation fault" when execute the program.


    First thing to note, programs often APPEAR to run well but in reality they
    have a bug. Second, there are things categorized as 'undefined behaviour'
    in C language. It is entirely possible that on Solaris the undefined
    behaviour is consistently good but on Linux it is consistently bad. Third,
    there are things categorized as 'implementation defined'. It is possible
    that the implementation on Solaris does what the programmer wants but the
    implementation on Linux does something different.

    Without seeing the source code it would be impossible to even guess at
    what is wrong. I wouldn't even be able to narrow down if it is problem 1,
    2 or 3.

    What you need to do is try to narrow down what the problem is. Use code
    inspections, a good debugger, whatever. If you still cannot figure it out
    you have hopefully narrowed it down enough that you can write a short
    program that exhibits the problem. Then you can post the short program to
    here and see if someone here can explain the problem.

    I'd also recommend you have a look at the FAQ for the newsgroup. It will
    teach you a lot of things that might be helpful as you are debugging the
    code. To find the FAQ go to www.google.ca and search for 'FAQ
    comp.lang.c'.

    Good luck.

    --
    Send e-mail to: darrell at cs dot toronto dot edu
    Don't send e-mail to
    Darrell Grainger, Oct 8, 2003
    #6
    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:
    849
  2. Pud
    Replies:
    0
    Views:
    562
  3. Replies:
    0
    Views:
    515
  4. Ivan Vecerina
    Replies:
    0
    Views:
    475
    Ivan Vecerina
    Jun 29, 2003
  5. Vasileios Zografos

    Re: segmentation fault exception handling

    Vasileios Zografos, Jun 30, 2003, in forum: C++
    Replies:
    5
    Views:
    15,571
    Pete Becker
    Jul 1, 2003
Loading...

Share This Page