I'm really having trouble spotting where my seg fault might be and I know a fresh set of eyes will help. Basically

start_ptr

is the head pointer and

start

is initialized to 0 and

end

is initialized to the number of nodes in the list. here's the code

node *curent = start_ptr;
 int mid;
 int i;  
 mid = (start+end)/2;
 for (i=0; i < mid; i++)
 {
  current = current->nxt;
 }
 if (current->emp.ID == key)
 printOne(current);
 else
  if (current->emp.ID < key)
   search (mid + 1, end , key);
   else
	search (start, mid - 1, key);

any idea where might my seg fault might be coming from? please help asap.

Recommended Answers

All 6 Replies

"Seg fault" is a bit vague. Seg fault on what line, with what Linked List, searching for what values? How many nodes in the linked list? Does the key exist in the Linked List? How many elements? Is the key smaller than the smallest element? Larger than the largest? What line number does this occur? Is the seg fault dereferencing a null pointer? Which null pointer? current? emp? Etc.

Stick some debugging statements in there. If it's happening in the for loop, you want to know whether it's gone past the end and isn't stopping or if something else is happening.

As a side note, why would you do a binary search with a Linked List? Seems more suited for an array.

"Seg fault" is a bit vague. Seg fault on what line, with what Linked List, searching for what values? How many nodes in the linked list? Does the key exist in the Linked List? How many elements? Is the key smaller than the smallest element? Larger than the largest? What line number does this occur? Is the seg fault dereferencing a null pointer? Which null pointer? current? emp? Etc.

Stick some debugging statements in there. If it's happening in the for loop, you want to know whether it's gone past the end and isn't stopping or if something else is happening.

As a side note, why would you do a binary search with a Linked List? Seems more suited for an array.

Because I have to use a binary search.

And my compiler doesn't tell me where the seg fault is, it just says seg fault.

as for everything else:
it's a linked list of struct and I'm testing the ID in each struct. the key is put in by the user but the one I'm testing with is toward the middle of the list. as far as the other stuff as I said it doesn't give any specifics as to what the seg fault means my compiler just says "segmentation fault"

Because I have to use a binary search.

And my compiler doesn't tell me where the seg fault is, it just says seg fault.

as for everything else:
it's a linked list of struct and I'm testing the ID in each struct. the key is put in by the user but the one I'm testing with is toward the middle of the list. as far as the other stuff as I said it doesn't give any specifics as to what the seg fault means my compiler just says "segmentation fault"

I don't see a condition where it says "not found". You need one.

Seg fault is a run-time error. It shouldn't have anything to with the compiler. You need to find out the answers to those questions in order to debug the program. Either use a debugger with breakpoints and look at the variable values or stick some cout statements in there (take 'em out later). You need to know exactly when and how it crashes. My hypotheses is that you're trying to dereference something that's null.

Well I just tried to put a cout at the very beginning of the function before I do anything and it wouldn't even print that out, it just seg faults automatically

Well I just tried to put a cout at the very beginning of the function before I do anything and it wouldn't even print that out, it just seg faults automatically

Well, that's valuable information. So it seg faults before you get to the code you posted? Move that cout statement earlier and earlier till you get it to display. That'll help pinpoint it.

Well the code I posted is in a function and every other function works fine. then it seg faults once that function is called

EDIT : I put the cout statement I mentioned at the as the first statement in said function

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.