Difference between concatenation and appendation
"岩倉 澪" via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Sun Jan 25 17:33:41 PST 2015
On Monday, 26 January 2015 at 01:17:17 UTC, WhatMeWorry wrote:
> Ok, I just made up that word. But what is the difference
> between appending and concatenating? Page 100 of TPDL says
> "The result of the concatenation is a new array..." and the
> section on appending talks about possibly needing expansion and
> reallocation of memory.
>
> But I still don't feel like I have a grasp on the subtleties
> between them. Can someone give a short and sweet "rule of
> thumb"?
>
> It might be so obvious that I'll regret posting this.
>
> Thanks.
I'm no expert, so take what I say with a grain of salt. That
said, here is my understanding:
When you append to an array with ~=, it attempts to reallocate
the array in-place, meaning it allocates on top of the already
used space, but grabs some more space past the end of the array.
If there isn't enough space after the array then obviously it
can't do that, so it allocates memory somewhere else that it can
fit and then it copies the contents of the array to the new
location. If you were to do myArray = myArray ~ moreStuff; I
assume this is no different from ~=. Conceptually ~= is just
syntactic sugar in the same way that += or -= is, you are just
doing a concatenation and then updating the array to point to the
new result. The fact that it can reallocate in place if there is
enough space is just like an optimization, in my mind.
More information about the Digitalmars-d-learn
mailing list