Re: Is it worth it to return a resource by reference?



Ney André de Mello Zunino wrote:
Hi.

I would like to know whether returning and passing around a resource (e.g. like those returned by mysql_query) by reference is worth it or nothing more than overkill. The reason I ask is because I don't know of what a resource is actually made and I fear it might be so simple (e.g. a mere number or pointer identifying a potentially big object) that passing it by reference could even increase the overhead. What's the truth?

The situation I have is a function which queries and returns a given range of objects (blog posts in this case) and another one which later receives the result set and works through it to output the contents. Something like the following sample code (using references):

// ----- BEGINNING OF SAMPLE CODE -----

function& getPosts(...)
{
return mysql_query(...);
}

function showPosts(&$postsResultSet)
{
while ($post = mysql_fetch_object($postsResultSet))
{
...
}
}

// driver code:
$results =& getPosts(...);
...
showPosts($results);

// ----- END OF SAMPLE CODE -----

Any comments will be appreciated.

Thank you,


Passing by reference should never increase the amount of overhead (at least not by any measurable amount), even for an integer. And for more complex objects it will decrease the overhead.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@xxxxxxxxxxxxx
==================
.



Relevant Pages

  • Re: pointer syntax
    ... > really passing a reference (which is like an old-school pointer, ... > a copy of the primitive's value and passing that. ... build them yourself with pointers. ... mean a var parameter, write a var parameter. ...
    (comp.lang.pascal.delphi.misc)
  • Re: Passing by reference
    ... It depends upon the reason one is passing the reference. ... instantiate all relationships via a constructor so there will always be ... I naturally think of 'getter' methods as 'knowledge ...
    (comp.object)
  • Re: Using ref
    ... the address of a variable that contains a reference to an object, ... the C version is passing the address of a variable that contains the   ... Inasmuch as the specification is unambiguous, there is a single truth. ... Every time he states the incorrect view, I will feel compelled to correct his incorrect statements. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Derived type argument to subroutine
    ... In practice, they often, but not always are. ... I can rely on the fact that arguments are passed by reference. ... Would the printed value always be 4, or is it undefined as the passing ... With an important exception, ...
    (comp.lang.fortran)
  • Re: Passing an object reference 1000 times
    ... Nicholas Paldino ... i do know i am passing a *reference* to the object; ... >> I think that the assumption you are making is that when you are>> passing a reference type around, you are copying it as you make the call ...
    (microsoft.public.dotnet.languages.csharp)