Solved

comparing pointers

Posted on 1997-05-04
9
327 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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

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…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

746 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

15 Experts available now in Live!

Get 1:1 Help Now