Newbie question: Return a locally allocated variable

Fitz fitz at figmentengine.com
Mon Sep 14 16:29:11 UTC 2020


I expect the following code below to create 10 items with 10 
different addresses, instead they all have the same address?

import std.stdio;

class Bob {
}

void main()
{
     for (auto i = 0; i < 10; i++) {
         auto pBob = bobFactory();
         writefln("bob @ %x\n", pBob);
     }
}

Bob *bobFactory() {
     Bob bob = new Bob;
     Bob *pBob = &bob;

     return pBob;
}


More information about the Digitalmars-d-learn mailing list