digitalmars.D.learn - Using .length returns incorrect number of elements
- QueenSvetlana (23/23) Aug 19 2018 When using the .length property of a dynamic array why does it
- Chris M. (5/29) Aug 19 2018 auto appendNumber = appender(arrayofNumbers);
- Chris M. (4/10) Aug 19 2018 Whoops
- QueenSvetlana (6/19) Aug 19 2018 New to d programming here :D Bare with me.
- Chris M. (2/25) Aug 19 2018 Yes, the .data field gives the array you're working on.
- Simen =?UTF-8?B?S2rDpnLDpXM=?= (24/47) Aug 19 2018 I suggest reading the D tour's page on arrays:
When using the .length property of a dynamic array why does it
return the incorrect number of elements after I use the appender?
import std.stdio;
import std.array : appender;
void main()
{
//declaring a dynamic array
int [] arrayofNumbers;
//append an element using the ~= syntax
arrayofNumbers ~= 1;
arrayofNumbers ~= 2;
//print the array
writeln(arrayofNumbers);
//Using appender
auto appendNumber = appender(arrayofNumbers);
appendNumber.put(10);
writeln(appendNumber.data);
writeln(arrayofNumbers.length);
}
Output:
[1, 2]
[1, 2, 10]
2 --- > Should be 3
Aug 19 2018
On Sunday, 19 August 2018 at 15:44:07 UTC, QueenSvetlana wrote:
When using the .length property of a dynamic array why does it
return the incorrect number of elements after I use the
appender?
import std.stdio;
import std.array : appender;
void main()
{
//declaring a dynamic array
int [] arrayofNumbers;
//append an element using the ~= syntax
arrayofNumbers ~= 1;
arrayofNumbers ~= 2;
//print the array
writeln(arrayofNumbers);
//Using appender
auto appendNumber = appender(arrayofNumbers);
appendNumber.put(10);
writeln(appendNumber.data);
writeln(arrayofNumbers.length);
}
Output:
[1, 2]
[1, 2, 10]
2 --- > Should be 3
auto appendNumber = appender(arrayofNumbers);
This returns a separate object. You probably meant to put this
for the last line
writeln(appendNumber.length);
Aug 19 2018
On Sunday, 19 August 2018 at 15:49:18 UTC, Chris M. wrote:On Sunday, 19 August 2018 at 15:44:07 UTC, QueenSvetlana wrote:Whoops writeln(appendNumber.data.length); https://run.dlang.io/is/4aNx1l[...]auto appendNumber = appender(arrayofNumbers); This returns a separate object. You probably meant to put this for the last line writeln(appendNumber.length);
Aug 19 2018
On Sunday, 19 August 2018 at 15:53:25 UTC, Chris M. wrote:On Sunday, 19 August 2018 at 15:49:18 UTC, Chris M. wrote:New to d programming here :D Bare with me. The object that is returned by appendNumber.data is an array reflecting the elements I added using appendNumber correct? So I would have the call length on appendNumber.data instead of the original array?On Sunday, 19 August 2018 at 15:44:07 UTC, QueenSvetlana wrote:Whoops writeln(appendNumber.data.length); https://run.dlang.io/is/4aNx1l[...]auto appendNumber = appender(arrayofNumbers); This returns a separate object. You probably meant to put this for the last line writeln(appendNumber.length);
Aug 19 2018
On Sunday, 19 August 2018 at 16:03:06 UTC, QueenSvetlana wrote:On Sunday, 19 August 2018 at 15:53:25 UTC, Chris M. wrote:Yes, the .data field gives the array you're working on.On Sunday, 19 August 2018 at 15:49:18 UTC, Chris M. wrote:New to d programming here :D Bare with me. The object that is returned by appendNumber.data is an array reflecting the elements I added using appendNumber correct? So I would have the call length on appendNumber.data instead of the original array?On Sunday, 19 August 2018 at 15:44:07 UTC, QueenSvetlana wrote:Whoops writeln(appendNumber.data.length); https://run.dlang.io/is/4aNx1l[...]auto appendNumber = appender(arrayofNumbers); This returns a separate object. You probably meant to put this for the last line writeln(appendNumber.length);
Aug 19 2018
On Sunday, 19 August 2018 at 16:03:06 UTC, QueenSvetlana wrote:On Sunday, 19 August 2018 at 15:53:25 UTC, Chris M. wrote:I suggest reading the D tour's page on arrays: https://tour.dlang.org/tour/en/basics/arrays In short, D arrays (more correctly called slices) are structs that look like this: struct Slice(T) { T* ptr; size_t length; } As the name Slice indicates, it represents a slice of memory, with a beginning address and a length. If you have two int[] instances (let's call them A and B) that point to the same data, they have their own copy of the ptr and length fields, and changing the value of a field in A will not change the corresponding field in B. When you append to A, what's happening* is the length is increased, and the appended data is written to the end of the memory pointed to. B does not see this, since it only looks at the first <length> elements of that block. -- Simen * Plus some checking of the length of the block of memory it's pointing to, and possible reallocation if the block isn't big enough.On Sunday, 19 August 2018 at 15:49:18 UTC, Chris M. wrote:New to d programming here :D Bare with me. The object that is returned by appendNumber.data is an array reflecting the elements I added using appendNumber correct? So I would have the call length on appendNumber.data instead of the original array?On Sunday, 19 August 2018 at 15:44:07 UTC, QueenSvetlana wrote:Whoops writeln(appendNumber.data.length); https://run.dlang.io/is/4aNx1l[...]auto appendNumber = appender(arrayofNumbers); This returns a separate object. You probably meant to put this for the last line writeln(appendNumber.length);
Aug 19 2018









Chris M. <chrismohrfeld comcast.net> 