Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 337
  • Last Modified:

comparing pointers

   How to compare two pointers?

    ie.

procedure checksum(...);
var
x: ^word;
y: ^word;
sum: Longint;
begin
  x := @first_procedure;
  y := @next_procedure;
  sum := 0;

  repeat
    sum := sum + x^;
    Inc(x);
  until x > y

end;

    Compiler reports error after "until".
0
biber
Asked:
biber
  • 5
  • 4
1 Solution
 
gysbert1Commented:
Type cast your word pointers as normal pointers, e.g.

until pointer(x)>pointer(y)
0
 
biberAuthor Commented:
Well, it seems that is not solution :(

Delphi says "Operand types do not match operator"

Any other idea?
0
 
gysbert1Commented:
Please let me know what you are doing here ...

What is first_procedure and next_procedure ? Are they word variables that you are trying to compare ? Why don't you compare the values themselves ?

If you explain a bit more I can try and find a solution for you.

PS It is possible to compare pointers. If this is necesarry I will go and have a look what Delphi wants. I suspect there is another and better way to do this ;^)

 
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
biberAuthor Commented:
Sorry, I should put some comments.

Procedure "Checksum" should calculate sum of memory locations
between two procedures (I called them "first_procedure" and
next_procedure")

Delphi generates "Operand types do not match operator" error
no matter if I define "x" and "y" as "x, y: ^word" or
"x, y: pointer"

>> PS It is possible to compare pointers. If this is necesarry I >> will go and have a look what Delphi wants. I suspect there is >> another and better way to do this ;^)

   Please do.

0
 
gysbert1Commented:
How about if you compare until x=y? That would cover everything between the two procedures, IF they are consecutively in memory. (If next_proc is before first_proc, the program will of course never end) OR, if you really want to check x>y, you can typecase each of them to a longint. (I actually didn't think with my original answer what you were doing, I know pointer comparison wrt > and < is not allowed, but = is allowed)
0
 
biberAuthor Commented:
> How about if you compare until x=y?

    Yes, that works, but Inc(x) (x is ^word) inrements x for one word (two bytes). What if @first_procedure is odd and @next_procedure is even? Loop will never end. Althought I am not sure about addresses, if they are always even.

     Anyway typecasting to longint works, and I guess that was solution.

There is another problem with procedure:
At "sum := sum + x^;"  compiler reports GPF. What could be the cause?

BTW How to "pay" you now for all this answers?
0
 
gysbert1Commented:
Is it the compiler that gives GPF, or when the program is run?
0
 
biberAuthor Commented:
Sorry, I made a mistake.
GPF is generated during run-time - just try example.
0
 
gysbert1Commented:
This is probably because the procedures are not aligned in memory. They are not forced to be and they might be movable in memory.

If you are still strugling with this I would suggest you post this as another question and maybe someone else could help you to solve it ;^)


0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 5
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now