| Thread overview | |||||||
|---|---|---|---|---|---|---|---|
|
January 07, 2015 Shouldn't the pointers be different | ||||
|---|---|---|---|---|
| ||||
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?
| ||||
January 07, 2015 Re: Shouldn't the pointers be different | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Nick | 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
| |||
January 07, 2015 Re: Shouldn't the pointers be different | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Nick | 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.
| |||
January 07, 2015 Re: Shouldn't the pointers be different | ||||
|---|---|---|---|---|
| ||||
Posted in reply to John Colvin | 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.
| |||
January 07, 2015 Re: Shouldn't the pointers be different | ||||
|---|---|---|---|---|
| ||||
Posted in reply to bearophile | 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 :)
| |||
Copyright © 1999-2021 by the D Language Foundation
Permalink
Reply