www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Static arrays passed by value..?

reply simendsjo <simen.endsjo pandavre.com> writes:
The spec for array says:
	Static arrays are value types. Unlike in C and D version 1, static 
arrays are passed to functions by value. Static arrays can also be 
returned by functions.

I don't get the "static arrays are passed to functions by value" part.

Here I am passing in a static and dynamic array. Both are passed by 
value, and the function can modify the array of both

	{
		void func(int[] arr, void* ptr) {
			arr[0] = 9;
			assert(&arr != ptr);
		}

		int[3] a = [1,2,3];
		func(a, &a);
		assert(a == [9,2,3]); // changed..
		
		int[] b = [1,2,3];
		func(b, &b);
		assert(b == [9,2,3]);
	}
Aug 07 2010
next sibling parent Lionello Lunesu <lio lunesu.remove.com> writes:
On 2010-08-07 9:26, simendsjo wrote:
 The spec for array says:
 Static arrays are value types. Unlike in C and D version 1, static
 arrays are passed to functions by value. Static arrays can also be
 returned by functions.

 I don't get the "static arrays are passed to functions by value" part.

 Here I am passing in a static and dynamic array. Both are passed by
 value, and the function can modify the array of both

 {
 void func(int[] arr, void* ptr) {
 arr[0] = 9;
 assert(&arr != ptr);
 }

 int[3] a = [1,2,3];
 func(a, &a);
 assert(a == [9,2,3]); // changed..

 int[] b = [1,2,3];
 func(b, &b);
 assert(b == [9,2,3]);
 }

When you use "int[]" as the parameter type, the array is passed by reference as a slice of the original array. If you write the function like this: fund(int[3] arr, void* ptr) {...} Now the array is passed by value and a copy is made.
Aug 07 2010
prev sibling next sibling parent "Simen kjaeraas" <simen.kjaras gmail.com> writes:
simendsjo <simen.endsjo pandavre.com> wrote:

 The spec for array says:
 	Static arrays are value types. Unlike in C and D version 1, static  
 arrays are passed to functions by value. Static arrays can also be  
 returned by functions.

 I don't get the "static arrays are passed to functions by value" part.

 Here I am passing in a static and dynamic array. Both are passed by  
 value, and the function can modify the array of both

 	{
 		void func(int[] arr, void* ptr) {

What your function signature is saying, is 'pass arr to me as if a dynamic array'. If you want to pass a static array, define the function like this, and you will see magic at work: void func( int[3] arr, void* ptr ) {
 			arr[0] = 9;
 			assert(&arr != ptr);
 		}

 		int[3] a = [1,2,3];
 		func(a, &a);

This call is equivalent to func( a[], &a );. Since your function says to treat the array as dynamic, a slice is created from your static array.
 		assert(a == [9,2,3]); // changed..
 		
 		int[] b = [1,2,3];
 		func(b, &b);
 		assert(b == [9,2,3]);
 	}

-- Simen
Aug 07 2010
prev sibling parent reply BCS <none anon.com> writes:
Hello simendsjo,

 The spec for array says:
 Static arrays are value types. Unlike in C and D version 1, static
 arrays are passed to functions by value. Static arrays can also be
 returned by functions.
 
 I don't get the "static arrays are passed to functions by value" part.
 
 Here I am passing in a static and dynamic array. Both are passed by
 value, and the function can modify the array of both

What that is saying is that "if the type of the function arg is a static array..." not "if the value passed to the function is a static array..." An example of a function taking a static array is: void Fn(int[5] arr){ ... } -- ... <IXOYE><
Aug 07 2010
parent simendsjo <simen.endsjo pandavre.com> writes:
On 08.08.2010 01:52, BCS wrote:
 What that is saying is that "if the type of the function arg is a static
 array..." not "if the value passed to the function is a static array..."

Yes, it would help to read the spec a bit closer :)
Aug 08 2010