On Friday, 8 September 2023 at 09:25:59 UTC, Hipreme wrote:
Hello, not completely unrelated to your problem, I have also
done something like that, and when you're in D, don't use
simply a pointer and length like that, use the `slice` operator.
See references:
https://tour.dlang.org/tour/en/basics/slices
https://dlang.org/spec/operatoroverloading.html#array-ops
For example, you can make your pointer a lot safer by doing:
```d
size_t length = 5;
int* pointer = cast(int*)malloc(int.sizeof * length); //Don't
int[] mallocArray = (cast(int*)malloc(int.sizeof *
length))[0..length]; //Do
```
On the second way, you'll get bound checks, thus, making it
safer. Also, no need to keep track of your length separately
anymore.
This is good practice in D language and you'll find yourself
using this instead in the future.
And yes, this works in betterC, it is a simple runtime check,
completely `@nogc @safe nothrow` and every other kind of thing
you would want.
Thank you for the reply! Using slices will give me the same
result (my code has another field that I forgot to mention btw
and it's `_len` (which holds the length that is used from the
capacity). However, this will just make the API worse to use so I
choose to not use slices in this case as using separate fields is
more convenient.