GC seems to crash my C-code function
frame
frame86 at live.com
Sat Sep 18 16:52:33 UTC 2021
On Saturday, 18 September 2021 at 11:47:52 UTC, Steven
Schveighoffer wrote:
> Have you tried:
>
> ```d
> const(char)* s2 = "...";
> ```
>
> This will work because string literals are zero terminated and
> implicitly castable to `immutable(char)*`, which will also
> implicitly cast to `const(char)*`.
>
> That should allow s2 to be reassigned but not modify the data.
> IIRC, string literal data even in C is put into a read-only
> section by many compilers, so the code shouldn't be changing it.
...
>
> The first rule of porting -- just translate, don't change
> anything. I would try to do exactly what C does without using
> the GC at all. Continue to use malloc/free. If you have issues
> with type representation, you may need to adjust for that.
This is what I try to achieve - not to change much.
But I see there is a mistake by me, s2 __is__ const in the
original C-code too.
Unfortunately, with `const(char)*`, `strchr()` did complain and
then I would have to cast it to `char*` - so I didn't used it in
first place because I really thought `.dup` wouldn't allocate
here.
There were also parts where the pointer is used in calculations -
which is accepted by the compiler - it just complains about
implicitly `long` to `char*` cast:
```
// const char *e
// char *w
out[p++] = ((w - e) + 3) % 40;
```
Why doesn't the compiler complain about
`char* - const(char)*`?
More information about the Digitalmars-d-learn
mailing list