Solved

an easy one

Posted on 2000-03-02
10
142 Views
Last Modified: 2010-04-04
hi all,

can one or more explain the meanings of following parameter-declarations and the effect for the calling procedure/function and the procedure/function itself

procedure P(var X : ATyp);

procedure P(const X : ATyp);

procedure P(X : ATyp);

not that i don't know it,
but had at last time some
discussions about that
and maybe someone has a better
explaination than i

meikl
0
Comment
Question by:kretzschmar
  • 4
  • 2
  • 2
  • +1
10 Comments
 
LVL 10

Accepted Solution

by:
Lischke earned 50 total points
ID: 2575732
Hi Meikl,

"an easy one" is probably not a good question title but as it is indeed easy I'll try my luck here :-)

Generally, you have the three choices given here to pass parameters to a function/procedure, but these ways don't describe the location of the parameter but the access mode. This means:

1) var parameters are passed via their reference (memory location) and can be altered
2) const parameters are directly passed values or references which cannot be altered
3) other parameters are copies of the original data put on the stack of the called function, they can be changed but all changes are lost on exit of the function

The consequences for the three ways are:

1) always a pointer is passed independent of the type of the parameter
2) parameters are passed via locations where they fit:
  - up to 32 bit values, including pointers and Int64 (if it can be split) (first 3 parameters only) are passed via EAX, EDX, ECX in that order
  - single, double and extended are passed via FPU stack
  - all other values are passed via the stack
3) like 2) but the compiler allows to use assignments on the values

I hope I forgot not too much :-)

Ciao, Mike
0
 
LVL 7

Expert Comment

by:RBertora
ID: 2575758
So tell me mike on what page of your new book on programming delphi will that appear?

Rob;-)
0
 
LVL 10

Expert Comment

by:Lischke
ID: 2575804
Hey Rob, long time no speak from you. I hope you are doing well!

Well, my preferences lie not in writing books or articles (I just canceled to write an OpenGL article for the german computer magazin c't) but to put my knowledge into superb software (and if possible, free software) :-)

Ciao, Mike
0
 
LVL 7

Expert Comment

by:RBertora
ID: 2575833
Yes, I was just kidding about the book, jsut my way of saying that was a superbly well put comment.
Happy to be back, (not with a vengance though) just browsing about these days.

C ya

Rob;-)
0
 
LVL 27

Author Comment

by:kretzschmar
ID: 2575837
wow,
looks good, mike
(also learned something,
didn't know the exact registers)

well, i will left this q one day to hear other explaination from other experts.

to the other experts,
be free, to comment the explaination in your words. if there are more than one good explaination (as now the one) i will offer seperate q(s) for this expert(s).

meikl
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 20

Expert Comment

by:Madshi
ID: 2576654
Hmmm... I thought using "const" would mean the same as "var", except that the Delphi compiler doesn't accept assignments/changes of this parameter?

What about this code (didn't test it):

type TBuffer = array [0..9999] of integer;

procedure HackBuffer(const buf: TBuffer);
begin
  buf[0] := 0;  // doesn't work
  PInt(@buf)^ := 0;  // works, is this change lost on exit? I'm not sure...
end;

This one should be added:

procedure P(out X : ATyp);

Which is the same as "var", but Delphi can give out more warnings, like "X may not be defined", if X is not set in P.

Even more interesting are parameters like these, I think:

procedure P(X: array of integer);
procedure P(var X: array of integer);
procedure P(const X: array of integer);

Regards, Madshi.
0
 
LVL 10

Expert Comment

by:Lischke
ID: 2576769
Hallo Madshi, ich hab' schon auf dich gewartet :-)

Well, you are right, you can actually modify the buffer this way and the value is really stored in the buffer. But this doesn't conflict with my description. It is passed as reference and the compiler prevents you to change it directly.

The "out" parameter is a good point as it is useful specifically for interfaces where the parameter itself does not pass in a value but only pass out (and any valid interface passed into an out parameter is referenced down).

To your last parameters. Look at this code straight out of my Geometry unit:

function MakeAffineVector(V: array of Single): TAffineVector; assembler;

// creates a vector from given values
// EAX contains address of V
// ECX contains address to result vector
// EDX contains highest index of V

asm
              PUSH EDI
              PUSH ESI
              MOV EDI, ECX
              MOV ESI, EAX
              MOV ECX, EDX
              INC ECX
              CMP ECX, 3
              JB  @@1
              MOV ECX, 3
@@1:          REP MOVSD                     // copy given values
              MOV ECX, 2
              SUB ECX, EDX                   // determine missing entries
              JS  @@Finish
              XOR EAX, EAX
              REP STOSD                     // set remaining fields to 0
@@Finish:     POP ESI
              POP EDI
end;


You can see here that I have not mentioned all possible kind of parameters. Here the "single" values are passed just like any ordinary array (which could well be dynamically created on the stack by the compiler).

Ciao, Mike
0
 
LVL 20

Expert Comment

by:Madshi
ID: 2579192
Hi Mike!

>> Well, you are right, you can actually modify the buffer this way and the value is really stored in the buffer. But this doesn't conflict with my description.

Oh yes, you're right of course...   :-)   I read your comment again and now it strikes my eyes...

And thank you for your array parameter description, will be useful someday...   (-:   I didn't know that before.

Regards, Madshi.
0
 
LVL 10

Expert Comment

by:Lischke
ID: 2579290
The most interesting thing for me was the fact that the result vector is also passed via a register (in the function MakeAffineVector). But this depends on the number and kind of parameters. Open arrays always need a hidden parameter telling the upper bound of the array and if the function is part of a class then the so often mentioned implicit "self" parameter also influences the parameter location. In this case all parameters slide "down" one register. Self is passed in EAX, the next parameters in EDX and ECX. All other registers should always be considered as being unitialized and reserved (on exit the same values must be there as were on enter, the direction flag is btw. cleared on enter and must be cleared on exit, otherwise the application comes in serious trouble because the compiler expects it so!). Btw: Usually Delphi holds the reference of a class usually in EDI or ESI and loads it into EAX when calling a method (but this is not guaranteed, of course) but it describes why an application often so badly crashes when registers used in a method aren't restored properly.

Ciao, Mike
0
 
LVL 27

Author Comment

by:kretzschmar
ID: 2579556
well, closing the thread now.

thanks for the comments

madshi i post a separate q for you
0

Featured Post

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.

Join & Write a Comment

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…
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 video discusses moving either the default database or any database to a new volume.
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

743 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

10 Experts available now in Live!

Get 1:1 Help Now