1
votes

So I have this (almost working) program that uses pointers to dynamic arrays and pointers to records (double-linked list). I declare some lists in a procedure and use them in another by passing pointers to them. Because of some initial errors, I've been passing them as variables i.e. by reference, but on some pages it says that then "the subprogram might inadvertently alter the value of the pointer which will lead to strange results". So what does that mean? Should I not pass them by ref. at all?

I could declare them globally and then not have the need to pass them. But other than that how does passing 'pointers to records (lists)' work? Can I operate with them normally?

I also stumbled upon this problem, which is why I ask here. I declare new lists like this:

procedure CreateList(var first:point; c:byte);

begin
 new(first);
 first^.previous := NIL;
 first^.digit := c;
 first^.next := NIL;
end;

Then I used one elsewhere and after that it goes for deletion:

procedure DeleteList(var first:point);
var q:point;

 begin
  while first <> NIL do
    begin
     q:= first^.next;
     if first^.previous = NIL then writeln('back is right');
     if q = NIL  then writeln('q is right');
     writeln(first^.digit);
     dispose(first);
     if first = NIL then writeln('first is right');
     first:= q;
    end;
 end;

But it returns Error 216 at disposing. I've put some writelines to ensure it is declared properly. The node apparently isn't nil as it steps into the cycle. Then all three writelns are written which means that previous and next node are both nil and the digit returns 1. When I put writeln after dispose even without condition, it doesn't proceed so it must stop at disposing.. Any tips? I tried deleting the var and passing by value - didn't change anything.

When I tried running this procedure with another in a simple program, it worked.

1
Somebody wrote if I could post exact output. I can't get the output to somewhere I could copy it from. Anyway, here is the transcription of the last part: ('|' is for newline) back is right | q is right | 1 | Runtime error 216 at $7793D968 | $7793D968 | $7793D877 | $00409529 | $00409018 | $00404232 | $00404ABE | $00404D54 | $0040ABE1 - h4nek
If the problem isn't in the code shown, it must be in the other code. We don't have anything to run here. "RTE 216" is General Protection Fault/Sigsegv, IOW illegal memory access somewhere, so not really helpful. - Marco van de Voort
The whole code is pretty long unfortunately. I though maybe somebody will come up with what can cause this or have some tips. Because I honestly don't know why it is happening and if it's not because I'm using something "unsupported". I also tried changing the digit's stored byte to integer because it is declared as such, but it's still the same. @Nestedtype Ok I've rewritten them. How did you know they were in Czech? :) Google Translate? - h4nek
Yep google translate...but anyway I think that's using anything else than English is prohibited on S.O. - Abstract type
What is the definition of your point type? According to the documentation the pointer must by typed. - Ant_222

1 Answers

0
votes

One should pass a variable by reference when it is intended to be changed or when it is so large that passing it by value decreases performance. In the latter case the programmer is responsible for ensuring the procedure shall not modify it.