When you want to pass a big bundle of data - such as an array - into a function, it's far more efficient to pass in just a single value in the form of a pointer to the array. That's the address in memory at which the array starts and that single value can provide the key to all the data without havig to duplicate it. By passing in the address rather than a copy of the data, it also means that the C function can - if it wishes - modify the original copy of the data and not just a duplicate which would be lost when the operation of the function completes.
From yesterday's C Course, I've added an example of passing in the address of the start of an array to a function to our source examples - see
[here]. The parameter is passed in as
&stuff[0]
and picked up by the declaration
int * options
You'll want to do this so often that you can use a shorthand - if you just pass in the array, it automatically assumes that you mean the address of the first element, and you can also collect the parameter using array notation:
int options[]
See source code
[here].
If you don't know at compile time how big you need your array to be, you can allocate memory to be used as an array with a function such as
calloc; the resultant memory does not have its own variable name, so it has to be accessed only via pointers. I've extended the above examples into an equivalent using calloc - source code
[here]. There are further examples in the memory management module too - see
[here].
This is really a "C" example ... but a similar principle applies in Perl. If you pass in
@abc to a sub, then you're duplicating the data. If you pass in
\@abc then you're passing in a reference - that's in effect a sophisticated pointer.
(written 2010-06-30)
Associated topics are indexed under
C205 - C and C based languages - Arrays [3245] Collections in C and C++ - arrays, vectors and heap memory blocks - (2011-04-12)
[3144] Setting up arrays in C - fixed size at compile time, or dynamic - (2011-01-24)
[3121] New year, new C Course - (2011-01-05)
[3118] Arrays of arrays - or 2D arrays. How to program tables. - (2011-01-02)
[2002] New C Examples - pointers, realloc, structs and more - (2009-01-20)
[1614] When an array is not an array - (2008-04-17)
P217 - Perl - More than Simple Lists and Hashes! [3906] Taking the lead, not the dog, for a walk. - (2012-10-28)
[3577] How to do multidimensional arrays (or rather lists and hashes) in Perl - (2012-01-14)
[3444] Take the dog on a lead - do not carry her. Perl references. - (2011-09-17)
[3406] Not multidimentional arrays - but lists of lists. Much more flexible. Perl! - (2011-08-26)
[3399] From fish, loaves and apples to money, plastic cards and BACS (Perl references explained) - (2011-08-20)
[3105] Adventure with references to lists and lists of references - (2010-12-26)
[3072] Finding elements common to many lists / arrays - (2010-11-26)
[3007] Setting up a matrix of data (2D array) for processing in your program - (2010-10-21)
[2996] Copying - duplicating data, or just adding a name? Perl and Python compared - (2010-10-12)
[2877] Further more advanced Perl examples - (2010-07-19)
[2241] Perl references - $$var and \$var notations - (2009-06-15)
[1514] Autovivification - the magic appearance of variables in Perl - (2008-01-21)
[293] Course follow-ups - (2005-04-27)
[43] Hash of lists in Perl - (2004-09-09)
5682
Some other Articles
Learning about Regular Expressions in C through examplesString functions in CStaring a C course with Hello World - why?C Course exercise and sample answer - source in 2 filesJust pass a pointer - do not duplicate the dataSoftware versions used - June 2010Respecting our customers anonimityLorry Parking in MelkshamPerl - the duplicate key problem explained, and solutions offeredA course is more than just a chap giving a lecture