Jump to page: 1 2
Thread overview
Dynamic / resizable array type, and a crash problem
May 14, 2015
ivoras
May 14, 2015
Adam D. Ruppe
May 14, 2015
ivoras
May 14, 2015
Namespace
May 14, 2015
Adam D. Ruppe
May 14, 2015
ivoras
May 14, 2015
Adam D. Ruppe
May 14, 2015
rumbu
May 14, 2015
ivoras
May 15, 2015
anonymous
May 14, 2015
anonymous
May 14, 2015
What is the recommended dynamic array type in D? So far I found Array (specifically Array!string, as I need a dynamic array of strings), but my example program crashes:

https://gist.github.com/ivoras/2d7737c214c3dc937c28

The crash is at line 20:

core.exception.AssertError@/usr/include/dmd/phobos/std/container/array.d(334): Assertion failure
----------------
./markov() [0x80fbfd1]
./markov(uint std.container.array.Array!(immutable(char)[]).Array.Payload.insertBack!(immutable(char)[]).insertBack(immutable(char)[])+0x6f) [0x80fa997]
./markov(uint std.container.array.Array!(immutable(char)[]).Array.insertBack!(immutable(char)[]).insertBack(immutable(char)[])+0x36) [0x80fa866]
./markov(_Dmain+0x167) [0x80d7ce7]

This is on DMD32 D Compiler v2.067.1
May 14, 2015
I would just use a regular `string[]` array...
May 14, 2015
On Thursday, 14 May 2015 at 12:46:48 UTC, Adam D. Ruppe wrote:
> I would just use a regular `string[]` array...

Is it resizable? Somehow I didn't get that impression from the docs. Apparently it doesn't even have an "insert" method: http://dlang.org/phobos/std_array.html .
May 14, 2015
On Thursday, 14 May 2015 at 13:26:27 UTC, ivoras wrote:
> On Thursday, 14 May 2015 at 12:46:48 UTC, Adam D. Ruppe wrote:
>> I would just use a regular `string[]` array...
>
> Is it resizable? Somehow I didn't get that impression from the docs. Apparently it doesn't even have an "insert" method: http://dlang.org/phobos/std_array.html .

----
string[] arr;
arr ~= "Foo";
arr ~= "Bar";

writeln(arr, ':', arr.length);
----

It's all built in. ;)

A nice article: http://dlang.org/d-array-article.html
and the language reference: http://dlang.org/arrays.html
May 14, 2015
On Thursday, 14 May 2015 at 13:26:27 UTC, ivoras wrote:
> Is it resizable?

You can append with the ~= operator and size down by slicing it.

> Apparently it doesn't even have an "insert" method: http://dlang.org/phobos/std_array.html .

http://dlang.org/phobos/std_array.html#insertInPlace

is the one you'd use for that.
May 14, 2015
On Thursday, 14 May 2015 at 12:42:01 UTC, ivoras wrote:
> https://gist.github.com/ivoras/2d7737c214c3dc937c28
>
> The crash is at line 20:
>
> core.exception.AssertError@/usr/include/dmd/phobos/std/container/array.d(334):
[...]
>
> This is on DMD32 D Compiler v2.067.1

Seems to be fixed in git head.
May 14, 2015
On Thursday, 14 May 2015 at 13:50:17 UTC, Adam D. Ruppe wrote:
> On Thursday, 14 May 2015 at 13:26:27 UTC, ivoras wrote:
>> Is it resizable?
>
> You can append with the ~= operator and size down by slicing it.
>
>> Apparently it doesn't even have an "insert" method: http://dlang.org/phobos/std_array.html .
>
> http://dlang.org/phobos/std_array.html#insertInPlace
>
> is the one you'd use for that.

Ok, string[] and ~= work and it doesn't crash now.

Where would I look for documentation on the "~=" operator? It's not described in http://dlang.org/phobos/std_array.html (though it is mentioned so you need to know what you are looking for before you find it...).

What would be the difference between Array!string and string[] ?
May 14, 2015
On Thursday, 14 May 2015 at 20:03:16 UTC, ivoras wrote:
> Where would I look for documentation on the "~=" operator?

http://dlang.org/arrays.html

under the heading "array concatenation"

> What would be the difference between Array!string and string[] ?

Array!string takes ownership of its own memory and frees it when no longer used making it appropriate in cases where you want that optmization. string[] is a slice - pointer plus length combination - into externally managed memory.
May 14, 2015
On Thursday, 14 May 2015 at 20:03:16 UTC, ivoras wrote:
>
> What would be the difference between Array!string and string[] ?

std.array is used to manipulate or create built-in arrays from various sources (ranges).

For basic needs, you can safely use built-in arrays: http://dlang.org/arrays.html

Concatenation operator (~) is documented here: http://dlang.org/arrays.html#array-concatenation
May 14, 2015
On Thursday, 14 May 2015 at 20:32:28 UTC, rumbu wrote:
> On Thursday, 14 May 2015 at 20:03:16 UTC, ivoras wrote:
>>
>> What would be the difference between Array!string and string[] ?
>
> std.array is used to manipulate or create built-in arrays from various sources (ranges).
>
> For basic needs, you can safely use built-in arrays: http://dlang.org/arrays.html
>
> Concatenation operator (~) is documented here: http://dlang.org/arrays.html#array-concatenation

Thanks, everyone!

I'm experimenting to get a feel for the language. Do you have a suggestion about this example code: https://goo.gl/F7LCAg to make it more "D-like", idiomatic?
« First   ‹ Prev
1 2