Passing dynamic arrays
Vladimir Panteleev
vladimir at thecybershadow.net
Mon Nov 8 09:40:45 PST 2010
On Mon, 08 Nov 2010 19:30:03 +0200, Jens Mueller <jens.k.mueller at gmx.de>
wrote:
> I find this behavior rather strange. Arrays are neither passed by value
> (copying the whole array) nor by reference. I see reasons for doing it
> like this, e.g. doing array = array[1..$] inside should not affect the
> outside.
Compare to this C-ish program:
void foo(int *array, int length) {
array = (int*)realloc(array, (length += 1000) * sizeof(int)); // may copy
the array
array[0] = 1;
}
int* a = new int[1];
foo(a, 1);
assert(a[0] == 1); // fails if a needs to copied inside foo
C's realloc *may* copy the memory area being reallocated. Just like when
using realloc, it's something you must be aware of when using D arrays.
--
Best regards,
Vladimir mailto:vladimir at thecybershadow.net
More information about the Digitalmars-d
mailing list