Jump to page: 1 2
Thread overview
Where can find fix length array memory layout document
Jun 18, 2019
lili
Jun 18, 2019
Daniel Kozak
Jun 18, 2019
Daniel Kozak
Jun 18, 2019
Dennis
Jun 18, 2019
lili
Jun 18, 2019
Cym13
Jun 18, 2019
Cym13
Jun 19, 2019
lili
Jun 19, 2019
Cym13
Jun 19, 2019
lili
Jun 20, 2019
XavierAP
June 18, 2019
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.
June 18, 2019
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.
>


June 18, 2019
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


June 18, 2019
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
```

June 18, 2019
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
```
June 18, 2019
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.
June 18, 2019
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)();
```
June 19, 2019
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

June 19, 2019
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.
June 19, 2019
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.

« First   ‹ Prev
1 2