converting D's string to use with C API with unicode
Jack
jckj33 at gmail.com
Sat Dec 5 21:55:13 UTC 2020
I totally forget to malloc() the strings and array. I don't do C
has been a while and totally forget this, thank you so much guys
for your answer.
my code now look like this, still there's a memory corrupt. Could
anyone help point out where is it?
>struct ProcessResult
>{
> string[] output;
> bool ok;
>
> C_ProcessResult* toCResult()
> {
> import core.stdc.stdlib : malloc, free;
> import core.stdc.string : memcpy;
> import core.exception : onOutOfMemoryError;
> import std.encoding : transcode;
> auto mem = malloc(C_ProcessResult.sizeof);
> if(!mem) {
> onOutOfMemoryError();
> }
> auto r = cast(C_ProcessResult*) mem;
> r.ok = this.ok;
> r.outputLength = cast(int) output.length;
> r.output = cast(wchar**) malloc((wchar*).sizeof *
>output.length);
> if(!r.output) {
> onOutOfMemoryError();
> }
> foreach(i; 0..output.length) {
> wstring ws;
> transcode(output[i], ws);
> auto s = malloc(ws.length + 1);
> if(!s) { onOutOfMemoryError();
> }
> memcpy(s, ws.ptr, ws.length);
> r.output[i] = cast(wchar*)s;
> }
> return r;
> }
>}
More information about the Digitalmars-d-learn
mailing list