Is there an easy way to convert a pointer to malloc'd memory to an array?

Era Scarecrow via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Wed May 25 05:59:49 PDT 2016


On Wednesday, 25 May 2016 at 12:06:58 UTC, Adam D. Ruppe wrote:
> Slicing and indexing are both allowed (actually, that's a way 
> to disable bounds checking on an individual expression, see 
> tip: http://arsdnet.net/this-week-in-d/dec-06.html ) on 
> pointers, though it isn't considered @safe.
>
> Be careful though: it is still manually managed, so if you pass 
> it to some function that keeps a slice to your slice, you open 
> yourself up to a use-after-free bug!

  If I knew about this trick in the past, it's been years so it 
would have been easy to forget. Still awesome to rediscover. 
Rereading a lot of stuff.

  As for passing slices to functions, I generally only do that 
when I know it won't hold onto it (or try to resize it or 
anything weird), which then lets me use stack memory rather than 
heap memory for temporaries.


More information about the Digitalmars-d-learn mailing list