Connecting Tech Pros Worldwide Help | Site Map

Arrays not truly passed by reference in functions

  #1  
Old August 2nd, 2006, 08:35 AM
Newbie
 
Join Date: Jul 2006
Posts: 4
:confused: This is just an absurd piece of code. Can anyone please explain the reason?

Expand|Select|Wrap|Line Numbers
  1. //a function that nulls the array passed as parameter
  2. function Nullify(arrRef)
  3. {
  4.  arrRef = null;
  5. }
  6.  
  7. var arr = new Array();
  8. for(var i = 0; i < 5; i++)
  9.   arr[arr.length] = "myString" + i;
  10.  
  11. alert("Length Before : " + arr.length); //This gives 5 as expected
  12.  
  13. Nullify(arr);  //this should null the array
  14.  
  15. alert("Length After : " + arr.length); //STRANGE : This also gives 5. Should it not give a javascript error?
According to the above code, it seems that array "arr" is not passed by reference. However if i splice( ) 2 elements in the function Nullify( ), then that alert message gives 3. ???
  #2  
Old August 2nd, 2006, 06:29 PM
iam_clint's Avatar
Forum Leader
 
Join Date: Jul 2006
Location: Oklahoma
Posts: 1,077

re: Arrays not truly passed by reference in functions


I see your problem take a close look and you will to
Expand|Select|Wrap|Line Numbers
  1. function Nullify(arrRef)
  2. {
  3.  arrRef = null; // Assuming you are trying to nullify the var arr, Well you cannot do it this way you cannot use a variable as another variable get what i mean arrRef is not gonn reference to arr.
  4. }
  5.  
so this would be the way to fix it

Expand|Select|Wrap|Line Numbers
  1. <script>
  2. function Nullify()
  3. {
  4.  arr = '';
  5. }
  6.  
  7. var arr = new Array();
  8. for(var i = 0; i < 5; i++)
  9.   arr[arr.length] = "myString" + i;
  10.  
  11. alert("Length Before : " + arr.length); //This gives 5 as expected
  12.  
  13. Nullify();  //this should null the array
  14.  
  15. alert("Length After : " + arr.length); //STRANGE : This also gives 5. Should it not give a javascript error?
  16. </script>
  17.  
  #3  
Old August 3rd, 2006, 04:58 AM
Newbie
 
Join Date: Jul 2006
Posts: 4

re: Arrays not truly passed by reference in functions


Thanks for the reply clint.

As you said

Quote:
Well you cannot do it this way you cannot use a variable as another variable get what i mean arrRef is not gonn reference to arr
But I am passing "arr" as a parameter in the function Nullify. So inside the function should not arr and arrRef point to same thing?
Instead of doing
Expand|Select|Wrap|Line Numbers
  1. arrRef = null
If i do any other changes, for instance I use splice( ) method to delete an element from arrRef.
Expand|Select|Wrap|Line Numbers
  1. arrRef.splice(0,1)
These changes are reflected pretty fine, when I come out of the function Nulllify. The alert message gives the length of array as 4 now, which is what it should be.
Only arrRef = null does not work. Apart from this any changes in arrRef are reflected in arr.

I hope i made myself clear.
  #4  
Old August 3rd, 2006, 09:47 PM
iam_clint's Avatar
Forum Leader
 
Join Date: Jul 2006
Location: Oklahoma
Posts: 1,077

re: Arrays not truly passed by reference in functions


Yes and no take a look what the script you are trying does

ok so your calling the function nullify with the parameter of arr
and you got function nullify(arrRef)
so it sets arr to arrRef -- two completely separate variables

so now arrRef is an array and aar is an Array
think of it this way
arr = "a";
arrRef = arr;

now arr and arrRef both = a

down the line if i put arrRef = null;

alright so arrRef is null now but arr is still a.
  #5  
Old August 3rd, 2006, 09:52 PM
iam_clint's Avatar
Forum Leader
 
Join Date: Jul 2006
Location: Oklahoma
Posts: 1,077

re: Arrays not truly passed by reference in functions


If that didn't clarify my point try this script with the alerts in there.
Expand|Select|Wrap|Line Numbers
  1. <script>
  2. //a function that nulls the array passed as parameter
  3. function Nullify(arrRef)
  4. {
  5. alert("arrRef = " + arrRef);
  6. alert("arr = " + arr);
  7. arrRef = null;
  8. alert("arrRef = " + arrRef);
  9. alert("arr = " + arr);
  10. }
  11. var arr = new Array();
  12. for(var i = 0; i < 5; i++)
  13.   arr[arr.length] = "myString" + i;
  14.  
  15. Nullify(arr);  //this should null the array
  16.  
  17.  
  18. </script>
  19.  
  #6  
Old August 4th, 2006, 07:02 AM
Newbie
 
Join Date: Jul 2006
Posts: 4

re: Arrays not truly passed by reference in functions


Ok thanks dear. Got the point.
Reply


Similar Threads
Thread Thread Starter Forum Replies Last Post
gcc: pointer to array Alexei A. Frounze answers 204 June 9th, 2006 11:15 AM
comp.lang.c Answers to Frequently Asked Questions (FAQ List) Steve Summit answers 0 November 13th, 2005 11:37 PM
comp.lang.c Answers to Frequently Asked Questions (FAQ List) Steve Summit answers 0 November 13th, 2005 09:56 PM
comp.lang.c Answers to Frequently Asked Questions (FAQ List) Steve Summit answers 0 November 13th, 2005 03:15 AM