Solved

comparing pointers

Posted on 1997-05-04
9
328 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
 

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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Copy file in dll not working but working on exe ! 18 87
Mydac connection data base issue 3 135
Convert Jpg /PNG To GIF 5 117
Dynamically Created Query 3 48
A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.
This is a video describing the growing solar energy use in Utah. This is a topic that greatly interests me and so I decided to produce a video about it.

948 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now