port of redo-c to BetterC

Dennis dkorpel at gmail.com
Thu Jun 10 10:04:38 UTC 2021


On Thursday, 10 June 2021 at 09:53:36 UTC, aquaratixc wrote:
> there is a function ```redo_ifchange (int targetc, char ** 
> targetv)``` inside which the ```char [targetc] skip;``` array 
> is defined;

That's a Variable-length array which D doesn't support.
If `targetc` has an upper bound, you can use a static array 
instead:
```D
import core.stdc.stdlib;

void redo_ifchange (int targetc, char ** targetv) {
     char[4096] buf = void;
     assert(targetc <= buf.length);
     char* skip = buf.ptr;
     // code
}
```

For larger sizes, you can use heap-allocation:
```D
import core.stdc.stdlib;

void redo_ifchange (int targetc, char ** targetv) {
     char* skip = cast(char*) malloc(targetc * char.sizeof);
     scope(exit) free(skip);
     // code
}
```

And if you really want variable-size stack allocation, you can 
use `alloca`:
```D
import core.stdc.stdlib;

void redo_ifchange (int targetc, char ** targetv) {
     char* skip = cast(char*) alloca(targetc * char.sizeof);
     // code
}
```
Though I don't recommend it.


More information about the Digitalmars-d mailing list