-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On 24/01/16 02:19, Bart Smissaert wrote:
> You didn't mention it but how arguments are called from VB6 is also
> very important, that is ByVal or ByRef. I think that unless the
> argument is going to be altered I have to pass them always ByVal.

Sort of.  You need to look at the C interface.  ByVal makes a copy of
the value and passes that to the function.  The function can do whatever
it wants to the copy as it won't affect the caller's version.  Byref
passes a pointer to the value in memory.  The called function has to
dereference the pointer to get the value at that location.  It can also
modify the value at that location, affecting the caller.  That ties in
with your rule of thumb.

But they are not interchangeable.  Randomly specifying one or the
other and seeing if it works is not a good idea.  Sometimes you do the
wrong one but can get lucky, or more likely crash/corrupt memory.

As an analogy, it is the difference between handing you a photocopy of
a document versus giving you a mailbox number that has a document
inside.  But realise that a mailbox is very different than what is
inside, and it is especially the case that they can be very different
sizes (eg it could be a big package inside the mailbox with a small
number).  C programmers will use ByRef if they want the item to be
modified, but can also do so if the item is larger since a mailbox
(pointer) number takes less space than the larger item.

In C syntax an integer is written as 'int' while a pointer to an
integer is written as 'int *' (the star is typically pronounced as
pointer).  Your rule should be ByVal when there are no '*' and ByRef
when there are.  The rules are non-obvious when you get more
complicated combinations of types and pointers.  Fortunately there is
a site that turn them into English for you.

  http://www.cdecl.org/

Try the following:

  int x;

  int *x;

Roger

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2

iEYEARECAAYFAlalMEQACgkQmOOfHg372QS1NACgkBqpEHb4q/XxAMgrfBDe/EMj
6+QAn2qDOgHITU8lrm68DiyIC62g06bb
=I6gu
-----END PGP SIGNATURE-----

Reply via email to