digitalmars.D.learn - Shouldn't the pointers be different
- Nick (18/18) Jan 07 2015 When i try to run the following code
- bearophile (23/24) Jan 07 2015 What you are printing is the address of the reference in the
- Nick (3/27) Jan 07 2015 It works!
- John Colvin (8/26) Jan 07 2015 I don't know the mechanism by which they are the same (optimiser
- John Colvin (3/35) Jan 07 2015 Sorry, my mistake, bearophile is correct. Nonetheless, you
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?
Jan 07 2015
Nick:The two nodes have the same address, is this right?What you are printing is the address of the reference in the stack frame of add(). If you want to print the reference you can use this: import std.stdio; class Test(T) { static class Node { T v; } void add(T e) { auto n = new Node; writeln("New node address: ", cast(void*)n); } } void main() { auto t = new Test!int; t.add(0); t.add(1); } Note also that class/struct/enum/union names in D start with an upper case. I have also used a static class instead. Bye, bearophile
Jan 07 2015
On Wednesday, 7 January 2015 at 10:56:07 UTC, bearophile wrote:Nick:It works! And thanks for the comments on the code :)The two nodes have the same address, is this right?What you are printing is the address of the reference in the stack frame of add(). If you want to print the reference you can use this: import std.stdio; class Test(T) { static class Node { T v; } void add(T e) { auto n = new Node; writeln("New node address: ", cast(void*)n); } } void main() { auto t = new Test!int; t.add(0); t.add(1); } Note also that class/struct/enum/union names in D start with an upper case. I have also used a static class instead. Bye, bearophile
Jan 07 2015
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.
Jan 07 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:Sorry, my mistake, bearophile is correct. Nonetheless, you shouldn't be surprised to see memory being re-used.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.
Jan 07 2015