#Why can i still access a pointer after it's been deleted?

1 messages · Page 1 of 1 (latest)

wary knot
#
#include <iostream>

struct L1 {
    int value;  
};

struct L2 {
    L1 *value;
    
    L2(L1 *value) : value(value) {}
    
    ~L2() { delete value; }
};


struct L3 {
    L2 *value;
    
    L3(L2 *value) : value(value) {}
    
    ~L3() { delete value; }
};

int main() {
    auto l1 = new L1{ 5 };
    auto l2 = new L2(l1);
    auto l3 = new L3(l2);
    delete l3;
    std::cout << "got here!" << std::endl;
    std::cout << l1->value << std::endl;        // segfaults? -- no
    std::cout << l2->value->value << std::endl; // segfaults?? -- no
    return 0;
}

I would expect this code to segfault in the lines marked segfaults? ...
It actually just outputs 0... is this just because it's actually undefined behaviour?
I'm using programiz online C++ compiler

fathom galeBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question run !howto ask.

wary knot
#

oh ok! it does actually segfault with g++ on my system.... so i guess i was right?

random vapor
#

It actually just outputs 0... is this just because it's actually undefined behaviour?
Yes. On some systems (or compiler settings) it segfaults, on some systems it doesn't. Both count as "undefined behavior"

topaz ravine
#

UB?

wary knot
#

!solved

fathom galeBOT
#

Thank you and let us know if you have any more questions!

wary knot
#

thanks 🙂

placid bay
# wary knot thanks 🙂

this is called a read after free bug, from the C function used to deallocate heap allocated memory called free, this once lead to a security vulnerability in IOS:

placid bay
#

!asan

coral lionBOT
#
How To Use Sanitizers

Sanitizers are tools which generate additional code in your program that can catch many common programming mistakes, such as:
accessing arrays out of bounds
signed integer overflows
race conditions

General Advice

Not all sanitizers can be combined, but when they can, use e.g.:
-fsanitize=address,undefined to combine them. Always compile with debug info to get line numbers, variable names, etc.

GCC 4.8+

-fsanitize=address
-fsanitize=undefined
-fsanitize=thread
-g for debug info

clang 3.1+
MSVC 19.27+ and VS 2019 16.9+

-fsanitize=address
-Zi for debug info

Sample Program
int main(void) {
    int x;
    return x;
} ```
`-fsanitize=memory -g` Output

SUMMARY: MemorySanitizer: use-of-uninitialized-value /tmp/test.cpp:3:5 in main
Exiting
(3:5 is line and column of return)

wary knot
#

thank you saved me an unrelated headache

placid bay
wary knot
#

oh, well i'm not even sure if it's related, tbh, I just started the video

#

but it made me think of the solution

placid bay
wary knot
#

speciifcally, i was deleting a struct, then reinitialising a new one, and it used the same spot in memor , so since i hadn't given default values

#

it just took the old ones

placid bay
#

because if you try to access something that is destroyed, you can get garbage data, or a crash