Re: Which is better, pass, or not pass by reference?

From: Marcel (neegeenspam_at_hotentottententententoonstellingen.nl)
Date: 10/21/03


Date: Tue, 21 Oct 2003 11:14:01 +0200


"Randell D." <you.can.email.me@randelld.at.yahoo.com> schreef in bericht
news:cU4lb.138072$6C4.80562@pd7tw1no...
>
> Folks,
>
> I've asked this before but never got any response but its important and I
> thought I'd pitch it again (hopefully a bit clearer)...
>
> One can pass data from one function to another either by passing a copy,
or
> passing by reference.
>
> My understanding of passing by reference (putting the & before a variable
> during the function declaration) means that the original data is used.
>
> My understanding of *not* passing by reference, (thus passing a copy)
> literally
> means creating a copy of the data contained in my variables - thus
doubling
> the memory usage for this data... even if its just a temporary while the
> function is being executed.
>
> I *believed* (past tense) that one should only pass by reference if they
> wanted the child function to change the data and pass the newer values
back
> to the parent function.
>
> I'm now thinking that this is just a feature and not the only usage. I am
> begining to think I should use "pass by reference" nearly all the time in
> order to conserve memory (since I won't be creating copies of my variable
> data in memory).
>
> Would this be good practice? Am I right that passing by reference means
that
> my variable data is not duplicated thus using less memory than if I had
> passed my variable data as a copy?
> --
> All comments + replies please via the newsgroup,
> Thanks,
> Randell D.

Passing by reference is usefull if you want a function to return more than
only one value... a function returns normally only one value (although you
can return an array with multiple results too)...

Using a reference is done if you want to change a var outside the scope of
your function....

Yes, interesting, why not always use call by reference??? Only thing i can
think is that you do not always want to change values outside the
function...

Regards,

Marcel



Relevant Pages

  • Which is better, pass, or not pass by reference?
    ... passing by reference. ... My understanding of passing by reference (putting the & before a variable ... the memory usage for this data... ...
    (comp.lang.php)
  • Re: performance hit using byref or byval?
    ... Byval might be faster if I don't mind the memory ... > You're not actually passing the huge object around when you use byref. ... > you're passing is the reference to the object, ...
    (microsoft.public.dotnet.framework.windowsforms)
  • Re: Which is better, pass, or not pass by reference?
    ... > or passing by reference. ... > doubling the memory usage for this data... ... Am I right that passing by reference means ... programming languages go farthest in this respect, ...
    (comp.lang.php)
  • [RFC] page replacement requirements
    ... Submitting too much I/O at once can kill latency and even lead to deadlocks when bounce buffers are involved. ... Must be able to deal with multiple memory zones efficiently. ... When on completion of the write to their backing-store the reference bit is still unset a callback is invoked to place them so that they are immediate candidates for reclaim again. ... For traditional page replacement algorithms this is not a big issue since we just implement per zone page replacement; ...
    (Linux-Kernel)
  • Re: Java or C++?
    ... >> CTips wrote: ... GC can only kick in when the last reference has ... If f was already bound to a lot of memory when it enters do-it then ... > more than the extra memory leaked because of sloppy manual deallocation]. ...
    (comp.programming)