0

can anyone help with this segmentation error i keep getting. this code is simple but the error is so hard to figure out.

struct Link {
  int key;
  unsigned data: 2;
  struct Link *next;
  struct Link *previous;
};

struct Link* addInOrder(struct Link *, struct Link);

int main() {
  struct Link *head;
  struct Link data1;
  struct Link data2;
  struct Link data3;
  data1.key = 25;
  data1.data = 1;
  data1.next = NULL;
  data2.key = 50;
  data2.data = 0;
  data2.next = NULL;
  data3.key = 100;  
  data3.data = 2; 
  data3.next = NULL;
  head = NULL;
  head = addInOrder(head, data2);
}

struct Link* addInOrder(struct Link *srt, struct Link l) {
  if(!srt) {
    return &l;
  }

  struct Link *temp = srt;
  while(temp->next && l.key > temp->key)
    temp = temp->next;

  printf("here\n");

  if(l.key > temp->key) {
    printf(" 1\n");
    temp->next = &l;
    l.previous = temp;
  }
  else {
    printf(" 2\n");
    l.previous = temp->previous;
    l.next = temp;
    printf( "2.2\n");
    if(temp->previous) {
      //printf("%i\n",temp->previous->key);
      temp->previous->next = &l;
    }
    printf(" 2.3\n");
    temp->previous = &l;
  }
  return srt;
}

i keep getting an error at the first line in addInOrder(). all the compiler says is Segmentation Error.

Edit: also, if i add printf("..."); right after the if statement and run it ... does not print

2 Answers 2

3

You're passing the second argument of addInOrder() by value (struct Link l). This creates a copy of the argument when you call the function, and in addInOrder(), l exists on the stack. You're then returning the address of the local variable and assigning it to head, but when the function exits, that variable is out of scope and is deallocated. So you're assigning an invalid address to head, and that results in a segfault.

Sign up to request clarification or add additional context in comments.

Comments

2

The part (and everywhere &l is used)

if (!srt)
    return &l;

is returning the address of a stack variable. Your addInOrder function should probably have the signature

struct Link* addInOrder(struct Link* srt, struct Link* l);

6 Comments

And then change all the struct references within the function (l.whatever) to pointer references (l->whatever).
hey james, if that doesnt work without changing main() than i must find another solution
@user1774515 The only thing you have to change in main is the line head = addInOrder(head, &data2); (change it to that). You'll have to make a couple of other changes in the body of addInOrder too, like @JimStewart pointed out. Just fix the compilation errors one by one from the top.
yeah i know i would have to change main, but im not allowed to change any part of main.
@user1774515 Why not, I don't see how it can work otherwise. Are you sure you copied it correctly? You only have to change one character in main.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.