Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

function parameter passing - copy or reference?

Posted on 2011-04-28
9
Medium Priority
?
346 Views
Last Modified: 2012-05-11
Quick and probably simple - but I'm c++

if function foo() takes scalar as parameters eg. sub foo { print "got: " . shift . "\n"; }
and I call it

$hashRef = { "0" => "blah blah" };
foo(${$hashRef}{'0'})

then calling foo makes the scalar string ${$hashRef}{'0'} copy or not?
Can I somehow verify it(in c++ i can using c-tor debugging)
0
Comment
Question by:ravenpl
  • 5
  • 4
9 Comments
 
LVL 85

Accepted Solution

by:
ozo earned 500 total points
ID: 35482220
If your function is
sub foo { print "got: " . shift . "\n"; }
then a copy will be made
but if  your function was something like
sub foo { $_[0]x=2 }
foo(${$hashRef}{'0'})
then you could verify that a reference to ${$hashRef}{'0'} was used
0
 
LVL 43

Author Comment

by:ravenpl
ID: 35482264
> sub foo { print "got: " . shift . "\n"; }
OK, true, copy has to be made creating new string, but
sub foo { print "got: ",  shift,  "\n"; }
?

Also, what the 'x' means in
> sub foo { $_[0]x=2 }


Actually I'm wondering if
my $mail = $spamassassin->parse(${$data_ref}{'body'}, 0);
makes a copy during the call already?
I'm asking because SA allows passing message source as reference to array(where I know i'm passing reference only).
0
 
LVL 85

Expert Comment

by:ozo
ID: 35482278
If $spamassassin->parse(${$data_ref}{'body'}, 0); makes a copy, it makes in the function, not in the call
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 43

Author Comment

by:ravenpl
ID: 35482292
ozo, are You familiar with SA internals? because sub foo { print "got: ",  shift,  "\n"; } will not make a copy - right?

Does switching to $spamassassin->parse(\@{$data_ref}{'body'}, 0); decreases the memory footprint?
0
 
LVL 43

Author Comment

by:ravenpl
ID: 35482310
Of course SA has to keep local copy of the message somehow. My concern is copying because of the function call
In c++ functions either accepts reference(no copy) or value(copy just because of calling the function)
0
 
LVL 85

Expert Comment

by:ozo
ID: 35482357
I think that sub foo { print "got: ",  shift,  "\n"; } will make a copy.

I wouldn't expect parse(\@{$data_ref}{'body'}, 0) to have much effect on the memory footprint,
but it really depends on how the function was written.

0
 
LVL 43

Author Comment

by:ravenpl
ID: 35482378
ozo, from Your words I understand that calling foo($scalar) will not make a copy of the $scalar(will pass as kind of internal reference). The foo function body can make it copied if coded recklessly - right?
last Q then, why sub foo { $_[0]x=2 } will not make a copy and what the 'x' stands for.
0
 
LVL 85

Expert Comment

by:ozo
ID: 35486260
perldoc perlop
...
       Binary "x" is the repetition operator.  In scalar context or if the
       left operand is not enclosed in parentheses, it returns a string
       consisting of the left operand repeated the number of times specified
       by the right operand.  In list context, if the left operand is enclosed
       in parentheses or is a list formed by "qw/STRING/", it repeats the
       list.  If the right operand is zero or negative, it returns an empty
       string or an empty list, depending on the context.


As you can observe by running the function, it modifies the original object.
0
 
LVL 43

Author Comment

by:ravenpl
ID: 35489543
Looks like 'my $x = shift' performs the copy but accessing via $_[0] is not.
Thanx.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans
Suggested Courses

810 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