473,320 Members | 2,029 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Arrays as Function Argument

nunoperalta
Hi, everyone! :)

I would like to know if an array, when given as function argument, is cloned (occupying more memory), or it is just referenced (but not modified as global variable).

I will explain:
$array = array(2, 3);
$var = $array; // this is reference, if I change $var, I will change $array

-------

And I am asking about this:

function checkArray($ar) {
return ($ar[1] > 0);
}

$array = array(1, 2, 3);
$var = checkArray($array);

In this case, will $array be recreated/cloned to checkArray()?
I just want to know, because I have a function that may *SOMETIMES* need to check two arrays, and I have always to give those arrays as argument, but I don't want to waste memory when those arrays are not needed...

Thanks in advance for your help!
Jul 20 '09 #1
5 2409
Dormilich
8,658 Expert Mod 8TB
@nunoperalta
I doubt that, references are usually made with the & operator
Expand|Select|Wrap|Line Numbers
  1. $var = &$array;
Jul 20 '09 #2
@Dormilich
this is deprecated in PHP6. I am sure about what I said in that part. You can test yourself :) Thanks, anyway.
Jul 20 '09 #3
Dormilich
8,658 Expert Mod 8TB
@nunoperalta
I tested i (though PHP 5.2.10) and yours didn't work.

and I'd like to know where this (about deprecation) is stated for data types other than objects (where this indeed is deprecated).
Jul 20 '09 #4
@Dormilich
wow... how come I tested this long ago and it worked before!!!
http://www.nunoperalta.com/rubbish/test.php
It failed, really!

That's ok then, I will use reference operator in the function arguments, then, so the arrays will not be cloned, right? Will it work even if the given arguments are null?

Thanks!

@Dormilich
well, I read about $var = &new Object(); , and I thought it was for all references in this way... $var = & *something*;
Sorry :)
Jul 20 '09 #5
Just tested again.
Only Objects are referenced automatically. Maybe I tested with an object, and thought it would be the same for Arrays.

I thought that, because other programming languages, like Python, do that.
Thanks again!
Jul 20 '09 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Jason | last post by:
I have a number of arrays that are populated with database values. I need to determine which array has the highest ubound out of all the arrays. The array size will always change based on the...
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...
3
by: T | last post by:
Hi all. I have a problem I have not been able to find a reference about. I am using VB6 and am only a hobbyist programmer. I have 7 arrays of type MyData. Type MyData has 23 elements. Which...
3
by: Randy Yates | last post by:
Hi, We know we can build arrays of variables of the same type and arrays of functions of the same "type" (i.e., same return value and same parameters), but is there a way to automate the calling...
11
by: truckaxle | last post by:
I am trying to pass a slice from a larger 2-dimensional array to a function that will work on a smaller region of the array space. The code below is a distillation of what I am trying to...
60
by: Peter Olcott | last post by:
I need to know how to get the solution mentioned below to work. The solution is from gbayles Jan 29 2001, 12:50 pm, link is provided below: >...
10
by: David Fort | last post by:
Hi, I'm upgrading a VB6 app to VB.net and I'm having a problem with a call to a function provided in a DLL. The function takes the address of a structure which it will fill in with values. I...
4
by: Christian Maier | last post by:
Hi After surfing a while I have still trouble with this array thing. I have the following function and recive a Segmentation fault, how must I code this right?? Thanks Christian Maier
29
weaknessforcats
by: weaknessforcats | last post by:
Arrays Revealed Introduction Arrays are the built-in containers of C and C++. This article assumes the reader has some experiece with arrays and array syntax but is not clear on a )exactly how...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.