C interface provides a pointer and a length... wrap without copying?
ketmar via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Sat Mar 11 14:45:11 PST 2017
cy wrote:
> So a lovely C library does its own opaque allocation, and provides access
> to the malloc'd memory, and that memory's length. Instead of copying the
> results into garbage collected memory (which would probably be smart) I
> was thinking about creating a structure like:
>
> struct WrappedString {
> byte* ptr;
> size_t length;
> }
>
> And then implementing opIndex for it, and opEquals for all the different
> string types, and conversions to those types, and then it occurred to me
> that this sounds like a lot of work. Has anybody done this already? Made
> a pointer/length pair, that acts like a string?
yep, it was done before.
int* a = cast(int*)malloc(1024);
auto b = a[0..1024];
// yay, b is just an ordinary slice now!
More information about the Digitalmars-d-learn
mailing list