954,500 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Segmentation Fault

This program is a double-linked list in C for my System's Software class.
Everything worked fine, until I coded the delete() and insert() functions.

The program compiles and executes on a window machine using Dev C++.
It also compiles without any errors or warnings on a linux machine using
gcc -g scan.c P3.c

However, when executing the a.out file, the program crashes with a segmentation fault.
My TA and I tried to find the bug for hours today, but to no avail. I would appreciate any help. Thanks.

Attachments P3.c (14.4KB) F.txt (0.38KB) scan.c (3.69KB) scan.h (0.13KB)
Strat79
Newbie Poster
1 post since Feb 2008
Reputation Points: 10
Solved Threads: 0
 
struct Object *delete(struct Object *start, int num)
{
      int i = 1;

counting in C and C++ always starts with 0, not 1.

I didn't get seg fault

The 7-th number in the list NUMBERS is '2'.
Deleted 10th element in list IDS.
Inserted 7-th element of NUMBERS into 10th index in list IDS.

IDS linked list:
I, 7, include
I, 5, stdio
I, 1, h
I, 7, include
I, 6, stdlib
I, 1, h
I, 6, define
I, 3, XYZ
I, 3, int
N, 1, 2
I, 4, main
I, 4, argc
I, 4, char
I, 4, argv
I, 3, int
I, 1, i
I, 1, j
I, 1, k
I, 3, int
I, 6, array1
I, 6, array2
I, 4, FILE
I, 5, Input
I, 5, fopen
I, 4, argv
I, 2, rb
I, 4, FILE
I, 6, Output
I, 5, fopen
I, 4, argv
I, 1, w
I, 3, int
I, 4, temp
I, 3, Int
I, 5, count
I, 3, int
I, 4, temp
I, 5, Array
I, 4, MaxL
I, 4, char
I, 3, str
I, 2, if
I, 5, Input
I, 4, NULL
I, 5, while
I, 4, feof
I, 5, Input
I, 6, fscanf
I, 5, Input
I, 1, d
I, 4, temp
I, 3, Int
I, 4, temp
I, 5, Array
I, 1, i
I, 4, temp
I, 3, Int

The 3-rd separator in the list SEPS is '<'.
Deleted 5th element in list NUMBERS.
Inserted 3-rd element of SEPS into 5th index in list NUMBERS.

NUMBERS linked list:
N, 2, 16
N, 1, 0
N, 1, 0
N, 7, 123.123
S, 1, <
N, 1, 0
N, 1, 2
N, 1, 0
N, 1, 0
N, 2, 10

Inserted '&&' into 1st index in list SEPS.

SEPS linked list:
S, 2, &&
O, 1, #
S, 1, white space
O, 1, <
O, 1, .
O, 1, >
S, 1, white space
O, 1, #
S, 1, white space
O, 1, <
O, 1, .
O, 1, >
S, 1, white space
O, 1, #
S, 1, white space
S, 1, white space
S, 1, white space
S, 1, white space
O, 1, (
S, 1, white space
O, 1, ,
S, 1, white space
S, 1, white space
O, 1, *
O, 1, [
O, 1, ]
O, 1, )
S, 1, white space
O, 1, {
T, 1, new line character
S, 1, white space
S, 1, white space
O, 1, =
S, 1, white space
O, 1, ,
S, 1, white space
S, 1, white space
O, 1, =
S, 1, white space
O, 1, ,
S, 1, white space
S, 1, white space
S, 1, white space
O, 1, =
S, 1, white space
O, 1, ;
S, 1, white space
S, 1, white space
O, 1, *
O, 1, ,
S, 1, white space
O, 1, *
O, 1, ;
S, 1, white space
S, 1, white space
O, 1, *
S, 1, white space
O, 1, =
S, 1, white space
O, 1, (
O, 1, [
O, 1, ]
O, 1, ,
S, 1, white space
O, 1, "
O, 1, "
O, 1, )
O, 1, ;
S, 1, white space
S, 1, white space
O, 1, *
S, 1, white space
O, 1, =
S, 1, white space
O, 1, (
O, 1, [
O, 1, ]
O, 1, ,
S, 1, white space
O, 1, "
O, 1, "
O, 1, )
O, 1, ;
T, 1, new line character
S, 1, white space
O, 1, _
S, 1, white space
O, 1, =
S, 1, white space
O, 1, ,
S, 1, white space
S, 1, white space
O, 1, =
S, 1, white space
O, 1, ;
S, 1, white space
S, 1, white space
O, 1, _
O, 1, [
O, 1, ]
O, 1, ;
S, 1, white space
S, 1, white space
O, 1, [
O, 1, ]
O, 1, ;
S, 1, white space
O, 1, (
S, 1, white space
O, 1, !
O, 1, =
S, 1, white space
O, 1, )
O, 1, {
S, 1, white space
O, 1, (
O, 1, !
O, 1, (
O, 1, )
O, 1, )
T, 1, new line character
O, 1, {
S, 1, white space
O, 1, (
O, 1, ,
S, 1, white space
O, 1, "
O, 1, %
O, 1, "
O, 1, ,
S, 1, white space
O, 1, &
O, 1, _
O, 1, )
O, 1, ;
S, 1, white space
O, 1, _
O, 1, [
O, 1, ]
S, 1, white space
O, 1, =
S, 1, white space
O, 1, _
S, 1, white space
S, 1, white space
O, 1, ;
S, 1, white space
S, 1, white space
S, 1, white space
S, 1, white space


UNKNOWN linked list:
U, 1, unknown character
U, 1, unknown character
U, 1, unknown character
U, 1, unknown character
U, 1, unknown character
U, 1, unknown character
U, 1, unknown character
U, 1, unknown character

Now delete half the objects in list UNKNOWN
Deleted 8-th element in list UNKNOWN.
Deleted 7-th element in list UNKNOWN.
Deleted 6-th element in list UNKNOWN.
Deleted 5-th element in list UNKNOWN.

New UNKNOWN linked list:
U, 1, unknown character
U, 1, unknown character
U, 1, unknown character
U, 1, unknown character
U, 1, unknown character

Press any key to continue . . .
Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

What did your core file tell you?

I don't have a linux machine to test it on and it is fine on my Sun, but it is probably a different architecture than you are using.

Generally Unix segmentation faults on a free are caused by over running a dynamic allocation. The failure will occur on a call to realfree. Once you find the allocation that is failing, you have to trace it back to the point where the array bounds were over written.

Try running Rational Purify on it if you can get a copy. It will show up as an array bounds write (ABW) problem.

rpiper138
Newbie Poster
11 posts since Sep 2008
Reputation Points: 56
Solved Threads: 1
 

Regrettably I have no time now to inspect your code more carefully but your list() initialization function is obviously wrong. It does initialize nothing because of it modifies by value copies of head/tail pointers only. It must have double indirection parameters to modify its arguments.

ArkM
Postaholic
2,001 posts since Jul 2008
Reputation Points: 1,234
Solved Threads: 348
 

You could try using valgrind on Linux to see if you have any memory errors. See http://valgrind.org/ .

stilllearning
Posting Whiz
309 posts since Oct 2007
Reputation Points: 161
Solved Threads: 43
 

Hi,
I am not able to run the program in Windows using Eclipse, it give me error...
To get more info on core file, comile your sources with -g option like
cc P3.c -c -g P3.o
and then when the core is generated run the command
gdb a.out core
It will give you the details as in where the segmentation fault occured... You may want to use where command when in gdb mode...

Will analyse your code and let you know whats the issue...

ahamed101
Junior Poster
117 posts since Jul 2008
Reputation Points: 51
Solved Threads: 14
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You