On Wednesday, 7 January 2015 at 10:56:09 UTC, John Colvin wrote:
On Wednesday, 7 January 2015 at 10:37:18 UTC, Nick wrote:
When i try to run the following code

import std.stdio;

void main(){
        auto a= new test!int();

        a.add(0);
        a.add(1);
}

class test(T){

        void add(T e){
                auto temp= new node();
                writeln("new node", &temp);
        }

        class node{
                T v;
        }
}
http://dpaste.dzfl.pl/c8e56b5954b8
The two nodes have the same address, is this right?

I don't know the mechanism by which they are the same (optimiser or garbage collector), but there's not reason why they shouldn't be. By the time you get to the second `new node()` the first one is completely unreachable, so why not just reuse the memory? Considering you don't initialise them differently, the object could even just be reused as-is.

Sorry, my mistake, bearophile is correct. Nonetheless, you shouldn't be surprised to see memory being re-used.

Reply via email to