Solved

comparing pointers

Posted on 1997-05-04
9
330 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
  • 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
ScreenConnect 6.0 Free Trial

Discover new time-saving features in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

 

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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Internet Explorer View Settings Question 15 111
delphi parse string to params 3 123
How to renew a Delphi rad-studio licence? 5 65
PHP preg_replace code convert to Delphi 14 57
The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …

778 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