Comment

Comment by lasagnaphil

This code, which is clearly undefined behavior (because of dangling pointer), outputs the number 2, which is very worrisome.

    #include <iostream>
    using namespace std;
    
    int* foo() {
        int y = 2;
        return &y;
    }
    int main() {
        int* x = foo();
        cout << *x << endl;
        return 0;
    }
I would recommend not using this for educational purposes yet; it could mislead students about pointer usage.
parent lasagnaphil 7 years ago

Discussion

Live from the official Hacker News API

A modern reader for Hacker News · SvelteKit + Skeleton