Difference between concatenation and appendation

Jonathan M Davis via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sun Jan 25 21:36:27 PST 2015


On Monday, January 26, 2015 01:17:15 WhatMeWorry via Digitalmars-d-learn 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.

~ is the concatenation operator. ~= is the append operator. ~ takes two
objects (most typically arrays, but user-defined types can define the same
operators) and returns a new one which contains the data from the first one
followed by the data from the second without affecting either object. ~=
takes two objects and adds the data from the second one to the end of the
first one (without affecting the second object). ~ is forced to allocate,
because it's creating a new object. Whether ~= allocates depends on the
data involved and the implementation, though ideally, it would avoid
allocation if it can.

In the case of arrays rather than user-defined objects, ~= is managed by the
GC. So, whether ~= allocates or not depends on whether there's room/capacity
in the block of memory that the array is a slice of after the array. If
there's enough room, then it will just increase the size of the array, and
put the new data in that memory, but if there isn't enough room (e.g.
because the memory block doesn't have enough room or because another array
refers to a pointer farther in the block of memory than the one being
appended to), then a new block of memory is allocated, the data is then
assigned to there, and the array is changed to be a slice of that memory
block. For user-defined types, it depends entirely on how ~= is implemented,
but it's probably going to either be doing something similar or be forced to
reallocate every time.

In any case, the main difference between ~ and ~= is that ~ creates a new
array or object _every_ time, and ~= mutates the first argument and will
generally only result in an allocation if it has to (particularly if you're
dealing with arrays).

I'd sugges that you read this article on D arrays:

http://dlang.org/d-array-article.html

It mixes up its terminology a bit with regards to dynamic arrays (the
language considers int[] to be a dynamic array, whereas the article refers
to the GC-allocated block of memory that the array refers to as being the
dynamic array), but it should definitely clarify a lot about D arrays for
you.

- Jonathan M Davis



More information about the Digitalmars-d-learn mailing list