Skip to main content
GameDev.net gamedev.net
🔒 Locked

[ Pascal ] Unbounded Queue, Put procedure

Started by Arjan B Dec 13, 2008 at 9:38 AM 4 replies 1k views
Original Post
Arjan B
Arjan B
Hey all, I'm having a problem with implementing a Put procedure for an unbounded queue. This is part of a homework assignment, but I've tried to implement it myself and can't make much sense out of the error message I'm presented so I thought I'd ask for help here ( it's weekend and have to turn this in Sunday evening so I can't ask a teacher/student-assistent and most of my friends are behind on their homework ;) ). What's Put supposed to do: Put a new value at the end of the queue.

procedure TQueueOfInt.Put(AValue: Integer);
var
  H: PCell; // auxiliary pointer to create new cell
begin
//# BEGIN TODO body of Put
  ...
//# END TODO
end;                                        


How did I try to implement this:

procedure TQueueOfInt.Put(AValue: Integer);
var
  H: PCell; // auxiliary pointer to create new cell
begin
//# BEGIN TODO body of Put
  New( H );
  H^.FNext     := nil;
  H^.FVal      := AValue;
  FTail^.FNext := H;
  FTail        := H;
//# END TODO
end;                                        


FTail: Points to last element in queue. FNext: Points to next element FVal: Value of element I allocate memory for H, set it's values, make the last element's next pointer point to this new element H, H is now the last element in the queue so make FTail point to that element. What error did I get: It all compiled fine but when I try to use the Put procedure I'm presented a messagebox with the following:
Quote:
Project Unbounded_Queues_App raised exception class 'External: SIGSEGV'.
So what are my questions: - What is wrong / What does this error message mean? - How do I solve this? Thank you in advance, Arjan
Arjan B
Arjan B
Sorry I forgot to mention I did that and tell you at which line it fails.

procedure TQueueOfInt.Put(AValue: Integer);var  H: PCell; // auxiliary pointer to create new cellbegin//# BEGIN TODO body of Put  New( H );  H^.FNext     := nil;  H^.FVal      := AValue;  FTail^.FNext := H; // <---- This one  FTail        := H;//# END TODOend;


I just don't see why it fails there..
rip-off
rip-off
What happens if the Queue is empty, and you try and add something to it. What value would you expect FTail to have in this case?
Arjan B
Arjan B
Just realized I'm dereferencing a pointer to nil.. Sorry to waste your time.. Thank you for your replies!
rip-off
rip-off
While not the most intuitive of errors, SIGSEGV usually means you are dereferencing a null pointer (or nil, depending on the language). It can also mean you are referencing memory you don't own.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.