View previous topic :: View next topic |
Author |
Message |
StringCheesian
Joined: 31 Mar 2005 Posts: 17
|
Posted: Sat Apr 09, 2005 11:59 am Post subject: Dyamic array syntax? Deep copy vs reference |
|
|
Given this: Code: | char[] a;
char[] b; |
Can you tell me how each of these different: Code: | a[] = b; // What does this do?
a = b[]; // Is this even valid?
// Is this the same as "a.length = b.length; a[0 .. b.length] = b[0 .. length];" ?
a[] = b[];
// Doesn't copy the data, so (after this) changing a will also change b, right?
a = b; |
I'm confused as to which is which - is there an easy way to remember? |
|
Back to top |
|
|
Chris Miller
Joined: 27 Mar 2004 Posts: 514 Location: The Internet
|
Posted: Mon Apr 11, 2005 6:22 pm Post subject: Re: Dyamic array syntax? Deep copy vs reference |
|
|
If there is slice syntax on the left then it will copy the elements. Both sides of the copy must have the same number of elements; resizing will not occur.
b[] is shorthand for b[0 .. foo.length], so having it on the right side of an assignment is useless, but on the left side it will imply copying elements.
a = b is a reference copy, so yes, modifying the elements of one will modify the other's.
If you simply want to duplicate an array you can use the array's dup property, which allocates new memory. |
|
Back to top |
|
|
StringCheesian
Joined: 31 Mar 2005 Posts: 17
|
Posted: Tue Apr 19, 2005 5:04 pm Post subject: |
|
|
Thank you! That makes sense. |
|
Back to top |
|
|
|