Shouldn't the pointers be different
John Colvin via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Wed Jan 7 02:59:39 PST 2015
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.
More information about the Digitalmars-d-learn
mailing list