DIP1000: Scoped Pointers (Discussion)
Walter Bright via Digitalmars-d
digitalmars-d at puremagic.com
Fri Aug 12 13:15:52 PDT 2016
On 8/11/2016 1:53 AM, John Colvin wrote:
> On Thursday, 11 August 2016 at 08:45:38 UTC, Walter Bright wrote:
>> On 8/11/2016 1:29 AM, John Colvin wrote:
>>> Can someone talk me through the lifetime algebra for the following?
>>>
>>> void foo()
>>> {
>>> int a;
>>> int** c;
>>> void bar()
>>> {
>>> int* b = &a; <= ok, b has a smaller lifetime than a
>>> c = &b; <= error, c has a larger lifetime than b
>>> }
>>> bar();
>>> *c; //undefined behaviour
>>> }
>
> but according to this rule:
>
> "For an unrestricted pointer, visibility is dictated by the usual lexical scope
> rules. Lifetime, however is dictated by the lifetime of the data to which the
> pointer points to."
>
> b should have the same lifetime as a, no?
When b is assigned the address of a, then the contents of b, as accessible
through b, have the lifetime of b.
More information about the Digitalmars-d
mailing list