Segmentation fault

A

Allan

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,
 
A

Artie Gold

Allan said:
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
 
M

Malcolm

Allan said:
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.
 
C

CBFalconer

Allan said:
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.
 
F

fermath

Malcolm said:
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
 
D

Darrell Grainger

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top