www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - new or no?

reply "Shadow_exe" <shadow_exe ukr.net> writes:
for(uint y=0; y<10; ++y){
	auto m = new Mutex();
	writeln(&m);
}

run:

7F66E5A05CF8
7F66E5A05CF8
7F66E5A05CF8
7F66E5A05CF8
7F66E5A05CF8
7F66E5A05CF8
7F66E5A05CF8
7F66E5A05CF8
7F66E5A05CF8
7F66E5A05CF8

As I understand it, all the time return one object
But:

auto m = new Mutex();
writeln(&m);
auto m1 = new Mutex();
writeln(&m1);

run:

7FCF60698CF0
7FCF60698CF8
Dec 23 2012
next sibling parent "Adam D. Ruppe" <destructionator gmail.com> writes:
On Sunday, 23 December 2012 at 19:43:54 UTC, Shadow_exe wrote:
 for(uint y=0; y<10; ++y){
 	auto m = new Mutex();
 	writeln(&m);
 }

That's the address of the local variable. The object it points to is somewhere else. An Object in D is more like an Object* in C++. Object* o = new Object(); &o == 0 o == 1 o = new Object(); &o == 0 // the local variable is still in the same place o == 2 // but it now points to a new object
Dec 23 2012
prev sibling next sibling parent Andrej Mitrovic <andrej.mitrovich gmail.com> writes:
On 12/23/12, Shadow_exe <shadow_exe ukr.net> wrote:
 for(uint y=0; y<10; ++y){
 	auto m = new Mutex();
 	writeln(&m);
 }

Use writeln(cast(void*)m) to get the address of the object, otherwise you're writing the address of the reference (which each time refers to a different object).
Dec 23 2012
prev sibling parent "Shadow_exe" <shadow_exe ukr.net> writes:
Yes, thank you!
Tired apparently, I need to rest...
Dec 23 2012