c++ - Will this lead to null pointer dereference? - Stack Overflow

admin2025-05-02  0

In the following code, a SomeClass object is scoped inside a block but a reference to it is stored in p in an outer block. And later using p, SomeMethod() of SomeClass is called.

SomeClass TestMethod(SomeClass c) {
    SomeClass * p;
    {
        SomeClass t;
        p = &t;
    }
    p->SomeMethod();
    return *p;
}

Will p->SomeMethod() fault due to a null pointer dereference?

I tried using int in place of SomeClass and didn't get a null pointer dereference. But I want to understand the behaviour according to the C++ standard.

In the following code, a SomeClass object is scoped inside a block but a reference to it is stored in p in an outer block. And later using p, SomeMethod() of SomeClass is called.

SomeClass TestMethod(SomeClass c) {
    SomeClass * p;
    {
        SomeClass t;
        p = &t;
    }
    p->SomeMethod();
    return *p;
}

Will p->SomeMethod() fault due to a null pointer dereference?

I tried using int in place of SomeClass and didn't get a null pointer dereference. But I want to understand the behaviour according to the C++ standard.

Share Improve this question edited Jan 2 at 22:03 Remy Lebeau 602k36 gold badges508 silver badges854 bronze badges asked Jan 2 at 0:13 OneHiveRuleOneHiveRule 111 silver badge4 bronze badges 7
  • 5 Taking the address of a stack variable and using it after its goes out of scope (in this case, t after the close of the inner braces) is undefined behaviour. I would assume that t is copy-constructed (or RVO'd) with whatever is returned by build(). p is still in scope when used, and definitely has the address of some out-of scope stack memory in it, so it won't be NULL. However, the instance of SomeClass has been destroyed, and we don't know what SomeMethod() does. As it's UB all bets are off. – marko Commented Jan 2 at 0:27
  • 8 Code won't even compile – Jarod42 Commented Jan 2 at 0:27
  • @OneHiveRule it would be instructive to know what was intended with this code - and particularly the inner braces. – marko Commented Jan 2 at 0:41
  • 2 If you want to discuss code, post it. This code doesn't compile so you can't reason about what it does. Specifically, "new" needs a type of object to create, whereas a builder tends to return an object instance; "new" returns a pointer but "SomeClass t" doesn't appear to be a pointer. What you seem to be angling for: IF you had Someclass* p2 = new SomeClass; p = p2; then the code would "work" (returning a copy of the heap-allocated object), but leak memory when TestMethod returns. – Tony Delroy Commented Jan 2 at 0:41
  • 2 SomeClass t = new SomeClass(); is very odd. Does SomeClass support doing this odd usage? (Without the code for SomeClass, it's hard to tell.) – Eljay Commented Jan 2 at 13:14
 |  Show 2 more comments

2 Answers 2

Reset to default 6

No, this will not necessarily lead to a null pointer dereference. p was assigned some non-null value within the block. Just because the thing it points at has ended its lifetime doesn't mean the value (pointer value) stored in p will become nullptr magically.

However, this is still undefined behavior if SomeMethod is a non-static method, because (assuming SomeClass is a class type - heavily implied) the lifetime of t ends at the end of the block.

The behavior is undefined.

By the time p->SomeMethod() is called, the object that p points at has already ended its lifetime, and p has been left dangling. p is not set to nullptr automatically, and dereferencing p for any reason will thus access an invalid object.

转载请注明原文地址:http://anycun.com/QandA/1746138528a92107.html