Re: Where can find fix length array memory layout document

2019-06-20 Thread XavierAP via Digitalmars-d-learn

On Tuesday, 18 June 2019 at 12:26:14 UTC, lili wrote:

Hi guys:
   Is the Dlang fix-length array alloc on stack?  when a test
writeln([1]).sizeof //16
writeln([2]).sizeof //16
Why, What is the fix-length array memory layout.


You are quite confused...

[...] is an array literal, not a static array. Those aren't the 
same thing.


When you pass a array literal anywhere in your code, it will in 
principle be referred as a slice variable. This will not 
reallocate the contents. However the slice reference is another 
variable that takes up two words of space (see code below).


This slice type is the same variable type that stores dynamic 
arrays -- be they allocated or null.


Array literals are not necessarily allocated. The compiler is 
free to embed them into the program machine code itself.


If you want a static array, you can just declare it directly e.g. 
int[n] arr. Of course you can also generate is out of an array 
literal with the staticArray std library function.


PS the layout of D arrays is of course linear and contiguous. 
Both static or dynamic, just like C/C++ static arrays or 
std::vectors respectively.


Hopefully this code makes things clear:

/*/
enum lenInts = int.sizeof;
static assert(lenInts == 4);

int[1] arrStatic;
static assert(lenInts == arrStatic.sizeof);

auto slice = arrStatic[];
alias sliceType = typeof(slice);
static assert(is(sliceType == int[]));

enum lenPointers = size_t.sizeof; // fyi (unsinged) pointers
static assert(ptrdiff_t.sizeof == lenPointers); // fyi signed 
pointer diff


static assert(sliceType.sizeof == 2 * lenPointers);
// because a D array reference remembers a pointer (like C) plus 
the length (stored in a word-length integer)




Re: Where can find fix length array memory layout document

2019-06-19 Thread lili via Digitalmars-d-learn

On Wednesday, 19 June 2019 at 12:53:05 UTC, Cym13 wrote:


Did you import it properly?

```
void main() {
import core.stdcpp.array;
auto a = array!(int, 4)();
}
```

compiles and runs without issue for me. You'll have to show 
your code if you want people to help you there.


Ok, where has some mistake in my code. thanks.



Re: Where can find fix length array memory layout document

2019-06-19 Thread Cym13 via Digitalmars-d-learn

On Wednesday, 19 June 2019 at 05:27:12 UTC, lili wrote:

On Tuesday, 18 June 2019 at 17:29:49 UTC, Cym13 wrote:

On Tuesday, 18 June 2019 at 17:25:42 UTC, Cym13 wrote:

On Tuesday, 18 June 2019 at 13:05:03 UTC, lili wrote:

On Tuesday, 18 June 2019 at 12:39:45 UTC, Dennis wrote:

[...]

Thanks a lot, where is a core.stdcpp.array , How to user it?
I test  but get a error
```
  auto aa = array!(int, 4); //error
```


Please don't shorten your code or errors to the point where 
there's hardly any information left: it's hard to help you if 
we can't know what you did and what went wrong.


Forgot to say that it's probably because you don't actually 
build an array here, try adding parentheses:


```
  auto aa = array!(int, 4)();
```


array!(int,4)(); compile occurs a error say: no overload 
matches for array


Did you import it properly?

```
void main() {
import core.stdcpp.array;
auto a = array!(int, 4)();
}
```

compiles and runs without issue for me. You'll have to show your 
code if you want people to help you there.


Re: Where can find fix length array memory layout document

2019-06-18 Thread lili via Digitalmars-d-learn

On Tuesday, 18 June 2019 at 17:29:49 UTC, Cym13 wrote:

On Tuesday, 18 June 2019 at 17:25:42 UTC, Cym13 wrote:

On Tuesday, 18 June 2019 at 13:05:03 UTC, lili wrote:

On Tuesday, 18 June 2019 at 12:39:45 UTC, Dennis wrote:

[...]

Thanks a lot, where is a core.stdcpp.array , How to user it?
I test  but get a error
```
  auto aa = array!(int, 4); //error
```


Please don't shorten your code or errors to the point where 
there's hardly any information left: it's hard to help you if 
we can't know what you did and what went wrong.


Forgot to say that it's probably because you don't actually 
build an array here, try adding parentheses:


```
  auto aa = array!(int, 4)();
```


array!(int,4)(); compile occurs a error say: no overload matches 
for array




Re: Where can find fix length array memory layout document

2019-06-18 Thread Cym13 via Digitalmars-d-learn

On Tuesday, 18 June 2019 at 17:25:42 UTC, Cym13 wrote:

On Tuesday, 18 June 2019 at 13:05:03 UTC, lili wrote:

On Tuesday, 18 June 2019 at 12:39:45 UTC, Dennis wrote:

[...]

Thanks a lot, where is a core.stdcpp.array , How to user it?
I test  but get a error
```
  auto aa = array!(int, 4); //error
```


Please don't shorten your code or errors to the point where 
there's hardly any information left: it's hard to help you if 
we can't know what you did and what went wrong.


Forgot to say that it's probably because you don't actually build 
an array here, try adding parentheses:


```
  auto aa = array!(int, 4)();
```


Re: Where can find fix length array memory layout document

2019-06-18 Thread Cym13 via Digitalmars-d-learn

On Tuesday, 18 June 2019 at 13:05:03 UTC, lili wrote:

On Tuesday, 18 June 2019 at 12:39:45 UTC, Dennis wrote:

On Tuesday, 18 June 2019 at 12:26:14 UTC, lili wrote:

[...]


I'm assuming you mean writeln([1].sizeof).
An array literal is a slice of a dynamic array (which is 
length + pointer, so 2*size_t size).
A fixed size array has to be declared as a local / member 
variable, and then the content is on the stack:


```
int[10] a;
writeln(a.sizeof); // 40
writeln(a[].sizeof); // 16 on 64-bit or 8 on 32-bit
```

To get a static array literal, you can use the library 
function staticArray:

```
import std.array;
writeln([1, 2, 3].staticArray.sizeof); // 12
```

Thanks a lot, where is a core.stdcpp.array , How to user it?
I test  but get a error
```
  auto aa = array!(int, 4); //error
```


Please don't shorten your code or errors to the point where 
there's hardly any information left: it's hard to help you if we 
can't know what you did and what went wrong.


Re: Where can find fix length array memory layout document

2019-06-18 Thread lili via Digitalmars-d-learn

On Tuesday, 18 June 2019 at 12:39:45 UTC, Dennis wrote:

On Tuesday, 18 June 2019 at 12:26:14 UTC, lili wrote:

Hi guys:
   Is the Dlang fix-length array alloc on stack?  when a test
writeln([1]).sizeof //16
writeln([2]).sizeof //16
Why, What is the fix-length array memory layout.


I'm assuming you mean writeln([1].sizeof).
An array literal is a slice of a dynamic array (which is length 
+ pointer, so 2*size_t size).
A fixed size array has to be declared as a local / member 
variable, and then the content is on the stack:


```
int[10] a;
writeln(a.sizeof); // 40
writeln(a[].sizeof); // 16 on 64-bit or 8 on 32-bit
```

To get a static array literal, you can use the library function 
staticArray:

```
import std.array;
writeln([1, 2, 3].staticArray.sizeof); // 12
```

Thanks a lot, where is a core.stdcpp.array , How to user it?
I test  but get a error
```
  auto aa = array!(int, 4); //error
```


Re: Where can find fix length array memory layout document

2019-06-18 Thread Daniel Kozak via Digitalmars-d-learn
On Tue, Jun 18, 2019 at 2:30 PM lili via Digitalmars-d-learn <
digitalmars-d-learn@puremagic.com> wrote:

> Hi guys:
> Is the Dlang fix-length array alloc on stack?  when a test
>  writeln([1]).sizeof //16
>  writeln([2]).sizeof //16
>  Why, What is the fix-length array memory layout.
>

When you do [1] without staticArray it will be automaticaly change to array
slice which consist of pointer to data (8bytes on 64bit) and length of
array again 8byte on 64bit


Re: Where can find fix length array memory layout document

2019-06-18 Thread Dennis via Digitalmars-d-learn

On Tuesday, 18 June 2019 at 12:26:14 UTC, lili wrote:

Hi guys:
   Is the Dlang fix-length array alloc on stack?  when a test
writeln([1]).sizeof //16
writeln([2]).sizeof //16
Why, What is the fix-length array memory layout.


I'm assuming you mean writeln([1].sizeof).
An array literal is a slice of a dynamic array (which is length + 
pointer, so 2*size_t size).
A fixed size array has to be declared as a local / member 
variable, and then the content is on the stack:


```
int[10] a;
writeln(a.sizeof); // 40
writeln(a[].sizeof); // 16 on 64-bit or 8 on 32-bit
```

To get a static array literal, you can use the library function 
staticArray:

```
import std.array;
writeln([1, 2, 3].staticArray.sizeof); // 12
```



Re: Where can find fix length array memory layout document

2019-06-18 Thread Daniel Kozak via Digitalmars-d-learn
import std.stdio;
import std.array : staticArray;


void main() {
writeln([1].staticArray.sizeof); //4
writeln([2,5].staticArray.sizeof); //8
}

On Tue, Jun 18, 2019 at 2:30 PM lili via Digitalmars-d-learn <
digitalmars-d-learn@puremagic.com> wrote:

> Hi guys:
> Is the Dlang fix-length array alloc on stack?  when a test
>  writeln([1]).sizeof //16
>  writeln([2]).sizeof //16
>  Why, What is the fix-length array memory layout.
>


Where can find fix length array memory layout document

2019-06-18 Thread lili via Digitalmars-d-learn

Hi guys:
   Is the Dlang fix-length array alloc on stack?  when a test
writeln([1]).sizeof //16
writeln([2]).sizeof //16
Why, What is the fix-length array memory layout.