Wrapping a C-style Array (Pointer + Length) in a Range Interface
ketmar via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Tue Jul 7 22:14:48 PDT 2015
On Tue, 07 Jul 2015 12:36:09 +0000, Rikki Cattermole wrote:
> I'm not aware of much docs regarding these sort of tricks. But I believe
> it was Adam who originally discovered this little trick.
> Atleast I'm pretty sure he is how I learnt about it. On D.learn as well.
i bet this trick was planned from the moment of introduction of slices in
D.
yet there is something that makes it dangerous: user is free to change
".length" of the "slice wrapper". and as GC doesn't manage the memory
slite is pointing to, it will copy slice contents. the program definitely
will not crash, but if copied data had some pointers to that copied data
(sometimes C program does this)... besides, one cannot use ".ptr" to `free
()` memory anymore.
so while there is nothing wrong in creating slices from pointers,
programmer should be aware of some potential pitfalls.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 181 bytes
Desc: not available
URL: <http://lists.puremagic.com/pipermail/digitalmars-d-learn/attachments/20150708/0eb2455b/attachment-0001.sig>
More information about the Digitalmars-d-learn
mailing list