Intelligent Scope Hierarchy

Namespace rswhite4 at googlemail.com
Fri Nov 1 01:59:34 PDT 2013


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.


More information about the Digitalmars-d-learn mailing list