FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Dyamic array syntax? Deep copy vs reference

 
Post new topic   Reply to topic     Forum Index -> General
View previous topic :: View next topic  
Author Message
StringCheesian



Joined: 31 Mar 2005
Posts: 17

PostPosted: Sat Apr 09, 2005 11:59 am    Post subject: Dyamic array syntax? Deep copy vs reference Reply with quote

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
View user's profile Send private message
Chris Miller



Joined: 27 Mar 2004
Posts: 514
Location: The Internet

PostPosted: Mon Apr 11, 2005 6:22 pm    Post subject: Re: Dyamic array syntax? Deep copy vs reference Reply with quote

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
View user's profile Send private message
StringCheesian



Joined: 31 Mar 2005
Posts: 17

PostPosted: Tue Apr 19, 2005 5:04 pm    Post subject: Reply with quote

Thank you! That makes sense.
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic     Forum Index -> General All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group