Thread overview | |||||||
---|---|---|---|---|---|---|---|
|
June 27, 2016 Local fixed sized arrays | ||||
---|---|---|---|---|
| ||||
I'm in need of a way to create a local array that isn't GC'ed. It must be dynamic in the sense of setting the size at compile time but it will be used only in scope and only on structs. function x(int y) { bool[y] arr; arr ~= 3; } I care about slicing or anything but appending, removal, and indexing. I don't even need bounds checking. I don't see a need to get locked in to the GC for such simple cases. |
June 27, 2016 Re: Local fixed sized arrays | ||||
---|---|---|---|---|
| ||||
Posted in reply to "Smoke" Adams | On 06/27/2016 02:58 PM, Smoke Adams wrote:
> I'm in need of a way to create a local array that isn't GC'ed. It must
> be dynamic in the sense of setting the size at compile time but it will
> be used only in scope and only on structs.
>
> function x(int y)
> {
> bool[y] arr;
>
> arr ~= 3;
>
> }
>
> I care about slicing or anything but appending, removal, and indexing. I
> don't even need bounds checking. I don't see a need to get locked in to
> the GC for such simple cases.
>
>
One way is to make x() a function template:
import std.stdio;
void x(int y)() {
bool[y] arr;
arr[y/2] = true;
writeln(arr);
}
void main() {
x!5();
}
Ali
|
June 27, 2016 Re: Local fixed sized arrays | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ali Çehreli | On Monday, 27 June 2016 at 22:56:35 UTC, Ali Çehreli wrote:
> On 06/27/2016 02:58 PM, Smoke Adams wrote:
>> I'm in need of a way to create a local array that isn't GC'ed. It must
>> be dynamic in the sense of setting the size at compile time but it will
>> be used only in scope and only on structs.
>>
>> function x(int y)
>> {
>> bool[y] arr;
>>
>> arr ~= 3;
>>
>> }
>>
>> I care about slicing or anything but appending, removal, and indexing. I
>> don't even need bounds checking. I don't see a need to get locked in to
>> the GC for such simple cases.
>>
>>
>
> One way is to make x() a function template:
>
> import std.stdio;
>
> void x(int y)() {
> bool[y] arr;
> arr[y/2] = true;
> writeln(arr);
> }
>
> void main() {
> x!5();
> }
>
> Ali
But the length depends on runtime behavior. Might be 5 or 100. This doesn't handle it, does it?
I already make a simple malloc based array that does what I want. Looks like a normal array with ~=, [], foreach. Does what I need it to do. Only works with BasicTypes of course.
|
June 27, 2016 Re: Local fixed sized arrays | ||||
---|---|---|---|---|
| ||||
Posted in reply to "Smoke" Adams | On 06/27/2016 04:02 PM, Smoke Adams wrote: > On Monday, 27 June 2016 at 22:56:35 UTC, Ali Çehreli wrote: >> On 06/27/2016 02:58 PM, Smoke Adams wrote: >>> I'm in need of a way to create a local array that isn't GC'ed. It must >>> be dynamic in the sense of setting the size at compile time but it will >>> be used only in scope and only on structs. >>> >>> function x(int y) >>> { >>> bool[y] arr; >>> >>> arr ~= 3; >>> >>> } >>> >>> I care about slicing or anything but appending, removal, and indexing. I >>> don't even need bounds checking. I don't see a need to get locked in to >>> the GC for such simple cases. >>> >>> >> >> One way is to make x() a function template: >> >> import std.stdio; >> >> void x(int y)() { >> bool[y] arr; >> arr[y/2] = true; >> writeln(arr); >> } >> >> void main() { >> x!5(); >> } >> >> Ali > > But the length depends on runtime behavior. You said "setting the size at compile time", so I got confused. :) > Might be 5 or 100. This doesn't handle it, does it? No, it doesn't handle it. However, if the maximum length is reasonably small, then you can create the largest array and use first part of it: import std.stdio; enum maxElements = 100; void foo(int y) { bool[maxElements] storage; bool[] x = storage[0..y]; x[y/2] = true; writeln(x); } void main() { foo(5); } However, the ~= operator on x would still involve the GC. > I already make a simple malloc based array that does what I want. Looks > like a normal array with ~=, [], foreach. That's basically the same as std.container.Array, which already comes with a bool specialization: import std.stdio; import std.container; void foo(int y) { auto x = Array!bool(); x.length = y; x[y/2] = true; writeln(x[]); } void main() { foo(5); } > Does what I need it to do. Only works with BasicTypes of course. I would expect yours to work with user-defined types as well. Ali |
June 30, 2016 Re: Local fixed sized arrays | ||||
---|---|---|---|---|
| ||||
Posted in reply to "Smoke" Adams | On Monday, 27 June 2016 at 21:58:04 UTC, "Smoke" Adams wrote: > I'm in need of a way to create a local array that isn't GC'ed. It must be dynamic in the sense of setting the size at compile time but it will be used only in scope and only on structs. `alloca` is made for that purpose. https://dlang.org/library/core/stdc/stdlib/alloca.html Search for it online to read-up on proper usage so you can avoid the caveats. cheers, Johan |
Copyright © 1999-2021 by the D Language Foundation