Thread overview
Variable length arrays under -betterC?
Apr 17, 2023
DLearner
Apr 17, 2023
user456
Apr 18, 2023
Salih Dincer
Apr 18, 2023
Ferhat Kurtulmuş
April 17, 2023

Requirement is to write some D code (which avoids the GC), that will be called from C.
So simple approach seemed to be to write D code under -betterC restrictions.

However, also need variable length arrays - but D Dynamic Arrays not allowed under -betterC.
So tried trivial example using std.container:

extern(C) void main() {

   import std.container;

   auto arr = Array!int(0, 2, 3);
}

compiled with:

dmd -betterC -run Array_ex_01v00

Which failed with:

C:\D\dmd2\windows\bin\..\..\src\druntime\import\core\internal\array\construction.d(207): Error: cannot use try-catch statements with -betterC
C:\D\dmd2\windows\bin\..\..\src\phobos\std\container\array.d(151): Error: template instance `core.internal.array.construction._d_arraysetctor!(const(Array!int)[], const(Array!int))` error instantiating
C:\D\dmd2\windows\bin\..\..\src\phobos\std\container\array.d(244):        instantiated from here: `RangeT!(const(Array!int))`
C:\D\dmd2\windows\bin\..\..\src\phobos\std\container\array.d(633):        instantiated from here: `RangeT!(Array!int)`
Array_ex_01v00.d(5):        instantiated from here: `Array!int`
C:\D\dmd2\windows\bin\..\..\src\druntime\import\core\internal\array\construction.d(207): Error: cannot use try-catch statements with -betterC
C:\D\dmd2\windows\bin\..\..\src\phobos\std\container\array.d(151): Error: template instance `core.internal.array.construction._d_arraysetctor!(immutable(Array!int)[], immutable(Array!int))` error instantiating
C:\D\dmd2\windows\bin\..\..\src\phobos\std\container\array.d(639):        instantiated from here: `RangeT!(immutable(Array!int))`
Array_ex_01v00.d(5):        instantiated from here: `Array!int`

Any ideas how to implement variable length arrays under -betterC?

April 17, 2023

On Monday, 17 April 2023 at 18:34:19 UTC, DLearner wrote:

>

Requirement is to write some D code (which avoids the GC), that will be called from C.
So simple approach seemed to be to write D code under -betterC restrictions.

However, also need variable length arrays - but D Dynamic Arrays not allowed under -betterC.
[...]
Any ideas how to implement variable length arrays under -betterC?

You need a struct with operator overloads and libc realloc(), (very) basically

struct Array(T)
{
private:

    struct Payload
    {
        T* ptr;
        size_t length;
    }

    Payload p;

public:

    size_t length() {
        return p.length;
    }

    void length(size_t v) {
        import core.stdc.stdlib;
        p.ptr = cast(T*) realloc(p.ptr, v * T.sizeof);
    }

    ref T opIndex(size_t i) {
        return p.ptr[i];
    }
}

void main()
{
    Array!int a;
}

but you'll have to implement

  • ctors
  • concat assign
  • slice assign
  • copy construction
  • value type elem alignment
  • default elem construction
  • default elem destruction
  • etc.

to make that usable in on trivial cases.

Maybe try to find an already existing one;

https://github.com/gilzoide/betterclist
https://github.com/aferust/dvector
(maybe more in https://code.dlang.org/search?q=betterc)

April 18, 2023

On Monday, 17 April 2023 at 19:12:20 UTC, user456 wrote:

>

... but you'll have to implement

  • ctors
  • concat assign
  • slice assign
  • copy construction
  • value type elem alignment
  • default elem construction
  • default elem destruction
  • etc.

to make that usable in on trivial cases.

I understand from the thread this: D gives us -betterC but nothing from the Phobos. So he says, see what you have, do what the hell you want!

Am I wrong about this?

SDB@79

April 18, 2023
On 18/04/2023 1:33 PM, Salih Dincer wrote:
> I understand from the thread this: D gives us -betterC but nothing from the Phobos.  So he says, see what you have, do what the hell you want!
> 
> Am I wrong about this?

Nope.

-betterC means you have no runtime or standard library support.

You are very much on your own.
April 18, 2023
On Tuesday, 18 April 2023 at 06:20:43 UTC, Richard (Rikki) Andrew Cattermole wrote:
> On 18/04/2023 1:33 PM, Salih Dincer wrote:
>> I understand from the thread this: D gives us -betterC but nothing from the Phobos.  So he says, see what you have, do what the hell you want!
>> 
>> Am I wrong about this?
>
> Nope.
>
> -betterC means you have no runtime or standard library support.
>
> You are very much on your own.

With betterC, some Phobos functionality work. For instance, most of std.range and std.algorithm will work. Problem is that there is no list of those in the docs. If something does GC allocations (std.container) or throws exceptions, it will not work. Based on my experience, -betterC, as its name implies, still provides a good programming experience rather than C.



April 18, 2023
On 4/17/23 11:34 AM, DLearner wrote:
> Requirement is to write some D code (which avoids the GC), that will be called from C.
> So simple approach seemed to be to write D code under -betterC restrictions.
> 
> However, also need variable length arrays - but D Dynamic Arrays not allowed under -betterC.

> Any ideas how to implement variable length arrays under -betterC?

variable-length arrays (i.e. slices) are valid in betterC. What isn't allowed is *GC-allocating* them and *appending* to them in betterC.

```d
int[] arr; // fine
// arr ~= 5; // nope, uses GC
arr = (cast(int *)malloc(int.sizeof * 5))[0 .. 5]; // fine
arr = arr[0 .. 3]; // also fine, no allocations needed
arr = (cast(int*)realloc(arr.ptr, int.sizeof * 10))[0 .. 10]; // fine, but leaks the original pointer
free(arr.ptr); // fine
```
What you *likely* want is an array type that handles some of the lifetime issues, or at least some UFCS/factory functions to make life easier (the above code is as bad as C code (except for the slicing)). But variable-length arrays work just fine as-is.

As with any manual memory management system, you need to handle the lifetime more carefully and explicitly. There's a reason why reference counting is hard to implement.

-Steve