Intelligent Scope Hierarchy

Stretto uiy12345 at gmail.com
Fri Nov 1 09:49:27 PDT 2013


On Friday, 1 November 2013 at 08:59:36 UTC, Namespace wrote:
> On Friday, 1 November 2013 at 05:49:04 UTC, Stretto wrote:
>> On Thursday, 31 October 2013 at 22:03:18 UTC, Namespace wrote:
>>> The 'it' property is only some 'singleton' approach.
>>> You can write:
>>>
>>> void foo() {
>>>   auto buffer = Mallocator.allocate(42);
>>>   /// ... many code
>>> }
>>>
>>> And at the end of the scope buffer is cleared because 
>>> Mallocator's destructor call deallocateAll (if I'm not wrong).
>>
>>
>> That doesn't seem right? deallocateAll would deallocate all 
>> allocated objects? Even outside of foo's scope? Also, how 
>> would Mallocator.allocate know what to deallocate unless it 
>> kept a history? Why would the example code explicitly 
>> deallocate the object at the end of the scope if it were 
>> unnecessary?
>
> Sorry, I was tired. ^^
>
> That is the correct code:
>
> void foo() {
>     Mallocator m;
>     auto buffer = m.allocate(42);
> }
>
> If m get out of scope, it deallocates all allocated memory.

Ok, I can see that but what if we want a "global" allocator? Then 
we need an easy way to deallocate on scope exit. I see no way to 
do that implicitly without some like I've mentioned(the 
(de)allocator needs a way to add itself to the scope exit).


More information about the Digitalmars-d-learn mailing list