473,473 Members | 1,541 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Returning reference to an array

Hi,
I'm looking at returning a reference to an array I create within a
subroutine.

I could do this...

sub foo {
my @theArray;

<snip>

return \@theArray;
}

This works however I'm concerned I'm returning a reference to stack data. I
heard if I used 'local' rather than 'my' the array will always exist (it's
global), but using 'my' I'm creating a temporary stack based
array that will then be cleaned up when the subroutine returns, hence my
reference will be invalid.

So...my or local?

Thanks

Andy
Jul 19 '05 #1
1 7409
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

"Andrew Fleet" <ar***********@earthlink.net> wrote in
news:Yy***************@newsread4.news.pas.earthlin k.net:
I'm looking at returning a reference to an array I create within a
subroutine.

I could do this...

sub foo {
my @theArray;

<snip>

return \@theArray;
}

This works however I'm concerned I'm returning a reference to stack
data. I heard if I used 'local' rather than 'my' the array will always
exist (it's global), but using 'my' I'm creating a temporary stack
based array that will then be cleaned up when the subroutine returns,
hence my reference will be invalid.

So...my or local?


Use 'my'. You heard wrong. Perl data structures are cleaned up when
there are no more remaining references to them. When you declare
@theArray with 'my', a new data structure is created. When you return
\@theArray and presumably store that return value into a variable, there
is still an extant reference to the array. When all references to the
array go out of scope or are reassigned, then the array will be garbage-
collected.

For your future reference, comp.lang.perl is a defunct newsgroup. Please
post general perl questions to comp.lang.perl.misc; you'll get a better
response there.

- --
Eric
$_ = reverse sort $ /. r , qw p ekca lre uJ reh
ts p , map $ _. $ " , qw e p h tona e and print

-----BEGIN PGP SIGNATURE-----
Version: PGPfreeware 7.0.3 for non-commercial use <http://www.pgp.com>

iQA/AwUBP5u1mWPeouIeTNHoEQIm8ACePbY4ajAWypokHBdPDyAamP dz+NcAoJSX
kYDhlG/aEBKkK9BYsmP08671
=6UqS
-----END PGP SIGNATURE-----
Jul 19 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Nico | last post by:
Hello folks, I am currently storing a set of objects inside an array, $itemlist = array(); $itemlist = new item("myitem"); //... and I am looking to develop a search function, which...
6
by: Krackers | last post by:
How do you write a function which returns a reference to an array. I can only get a function to return a copy of the array itself. I've had a look at some other threads in this group an the return...
7
by: BrianJones | last post by:
Hi, if you have a function, how is it possible to return an array? E.g.: unsigned long function(...) // what I want to do, obviously illegal I do know such would be possible by using a dynamic...
5
by: Gent | last post by:
I have two questions which are very similar: Is it possible to return an object in C++. Below is part of my code for reference however I am more concerned about the concept. It seems like the...
10
by: Fraser Ross | last post by:
I need to know the syntax for writing a reference of an array. I haven't seen it done often. I have a class with a member array and I want a member function to return an reference to it. ...
3
by: Faustino Dina | last post by:
Hi, The following code is from an article published in Informit.com at http://www.informit.com/guides/content.asp?g=dotnet&seqNum=142. The problem is the author says it is not a good idea to...
2
by: Tany | last post by:
How can I declare function returning array of Integer pointers . Please help !!
11
by: jza | last post by:
Hello all, I am fairly new to c, coming from a Java background. I am working on a mathematical program and I have a function that needs to return a 2-d array. After some web searching, I have...
8
by: darren | last post by:
Hi everybody, have a quick look at this code: ===== ===== int main(void) { string msg; makeString(msg); cout << "back in main, result = " << msg << endl;
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.