digitalmars.D - Manual Memory Management Example for dlang.org Docs (Code Review)
- Mike (16/16) Feb 25 2014 As I study D, I am running across quite a few unmaintained
- Mike (4/6) Feb 25 2014 I modified this based on Adam D. Ruppe's example here(
- Dicebot (7/11) Feb 26 2014 I don't think it is a good thing to do. Of course GC is likely to
- Mike (5/17) Feb 26 2014 Thanks,
- Sean Kelly (3/11) Feb 27 2014 For certain standard errors, the proper approach is probably to
As I study D, I am running across quite a few unmaintained examples in dlang.org's documentation. Rather than leaving them for the next guy to step in, I'm (one-by-one) submitting pull request to fix them. I need an good manual memory management example, but the one at http://dlang.org/memory.html#newdelete uses the deprecated allocators. Daniel Gibson posted an example here (http://forum.dlang.org/post/ip76fa$2o7k$3 digitalmars.com) back in 2011. I've modified that example and posted a new one here (http://dpaste.dzfl.pl/f4b09b9e6c46) using destroy. I'm quite new to D, so I'm asking the community to please review this example so I can submit a pull and update the docs. Thanks, Mike
Feb 25 2014
On Wednesday, 26 February 2014 at 02:23:47 UTC, Mike wrote:I've modified that example and posted a new one here (http://dpaste.dzfl.pl/f4b09b9e6c46) using destroy.I modified this based on Adam D. Ruppe's example here( http://arsdnet.net/dcode/malloc.d). Please review this (http://dpaste.dzfl.pl/2377217c7870) instead.
Feb 25 2014
On Wednesday, 26 February 2014 at 04:43:30 UTC, Mike wrote:Please review this (http://dpaste.dzfl.pl/2377217c7870) instead.throw new Exception("Out of memory");I don't think it is a good thing to do. Of course GC is likely to have some memory left in hs internal pools but in general once you hit out-of-memory state last thing you want is not allocate even more.// Deallocate test heapDeallocate(test);More idiomatic D is to put `scope(exit) heapDeallocate(test);` immediately after allocation line.
Feb 26 2014
On Wednesday, 26 February 2014 at 15:00:13 UTC, Dicebot wrote:On Wednesday, 26 February 2014 at 04:43:30 UTC, Mike wrote:Thanks, I've made changes and I guess this is the one I'll use for the pull. (http://dpaste.dzfl.pl/31367860c005)Please review this (http://dpaste.dzfl.pl/2377217c7870) instead.throw new Exception("Out of memory");I don't think it is a good thing to do. Of course GC is likely to have some memory left in hs internal pools but in general once you hit out-of-memory state last thing you want is not allocate even more.// Deallocate test heapDeallocate(test);More idiomatic D is to put `scope(exit) heapDeallocate(test);` immediately after allocation line.
Feb 26 2014
On Wednesday, 26 February 2014 at 15:00:13 UTC, Dicebot wrote:On Wednesday, 26 February 2014 at 04:43:30 UTC, Mike wrote:For certain standard errors, the proper approach is probably to call the appropriate handler in core.exception.Please review this (http://dpaste.dzfl.pl/2377217c7870) instead.throw new Exception("Out of memory");I don't think it is a good thing to do. Of course GC is likely to have some memory left in hs internal pools but in general once you hit out-of-memory state last thing you want is not allocate even more.
Feb 27 2014