www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Shouldn't the pointers be different

reply "Nick " <nickbakkegaard gmail.com> writes:
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
next sibling parent reply "bearophile" <bearophileHUGS lycos.com> writes:
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
parent "Nick " <nickbakkegaard gmail.com> writes:
On Wednesday, 7 January 2015 at 10:56:07 UTC, bearophile wrote:
 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
It works! And thanks for the comments on the code :)
Jan 07 2015
prev sibling parent reply "John Colvin" <john.loughran.colvin gmail.com> writes:
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
parent "John Colvin" <john.loughran.colvin gmail.com> writes:
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.
Jan 07 2015