Solved

comparing pointers

Posted on 1997-05-04
9
333 Views
Last Modified: 2010-04-06
   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
Comment
Question by:biber
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
9 Comments
 
LVL 2

Accepted Solution

by:
gysbert1 earned 50 total points
ID: 1335877
Type cast your word pointers as normal pointers, e.g.

until pointer(x)>pointer(y)
0
 

Author Comment

by:biber
ID: 1335878
Well, it seems that is not solution :(

Delphi says "Operand types do not match operator"

Any other idea?
0
 
LVL 2

Expert Comment

by:gysbert1
ID: 1335879
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
Industry Leaders: 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!

 

Author Comment

by:biber
ID: 1335880
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
 
LVL 2

Expert Comment

by:gysbert1
ID: 1335881
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
 

Author Comment

by:biber
ID: 1335882
> 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
 
LVL 2

Expert Comment

by:gysbert1
ID: 1335883
Is it the compiler that gives GPF, or when the program is run?
0
 

Author Comment

by:biber
ID: 1335884
Sorry, I made a mistake.
GPF is generated during run-time - just try example.
0
 
LVL 2

Expert Comment

by:gysbert1
ID: 1335885
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

Industry Leaders: 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!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question