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

```d
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)
  • Variable length ... DLearner via Digitalmars-d-learn
    • Re: Variabl... user456 via Digitalmars-d-learn
      • Re: Var... Salih Dincer via Digitalmars-d-learn
        • Re:... Richard (Rikki) Andrew Cattermole via Digitalmars-d-learn
          • ... Ferhat Kurtulmuş via Digitalmars-d-learn
    • Re: Variabl... Steven Schveighoffer via Digitalmars-d-learn

Reply via email to