DIP1000: Scoped Pointers (Discussion)

John Colvin via Digitalmars-d digitalmars-d at puremagic.com
Thu Aug 11 01:29:37 PDT 2016


On Wednesday, 10 August 2016 at 20:36:38 UTC, Dicebot wrote:
> Proposal text: 
> https://github.com/dlang/DIPs/blob/master/DIPs/DIP1000.md

Can someone talk me through the lifetime algebra for the 
following?

void foo()
{
     int a;
     int** c;
     void bar()
     {
         int* b = &a;
         c = &b;
     }
     bar();
     *c; //undefined behaviour
}

I think it's this:

lifetime(a) = all of foo
lifetime(c) = infinite (because null) //initially
lifetime(b) = lifetime(&a) = lifetime(a) = all of foo // ????
lifetime(c) = lifetime(&b) = lifetime(b) = all of foo //on 
assignment in bar

That would suggest that dereferencing c was OK, because the 
lifetime is all of foo. What am I missing?


More information about the Digitalmars-d mailing list