Pointer can point local variable's memory outside it's scope?

Kaidul
void foo(int** ptr) {
    int value = 4;
    *ptr = &value;
//    **ptr = value;
}

int main(void) {
    int value = 7;
    int* ptr = &value;
    foo(&ptr);
    cout << *ptr << endl; // 4
    return 0;
}

My Question is - as the value = 4 is no longer valid/out of scope after returning from foo, why *ptr is showing 4 instead of some garbage value?

Drew McGowen

Because you're returning a pointer to a local variable, this is undefined behavior. This includes "appearing" to work, but it's a terrible idea to rely on it in the general case.

In this specific case, the value is left on the stack, and it appears the generated code fetches *ptr just after the call to foo, and before any other function calls. As such, the value has not been overwritten by any other function calls.

If you were to instead insert a function call between the foo(&ptr) and cout << ... statements, the value would more than likely be garbage.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Pointer can point local variable's memory outside it's scope?

From Java

Can a local variable's memory be accessed outside its scope?

From Dev

How to pass variable outside method's scope in react?

From Dev

What's the point of the local variable in Eiffel's attached-statement?

From Dev

What will a pointer to a object point to outside the object scope in C++

From Dev

Can I use values of array declared as local variable outside of its scope?

From Dev

Floating point variable's value missing in data memory

From Dev

What's the point of the pointer in MASM?

From Dev

Save pointer's memory address

From Dev

Get the scope's element outside the directive

From Dev

Acessing a locally declared struct outside of it's scope

From Dev

Reset value(s) of 'this' from outside their scope/function

From Dev

How can i use scope's variable in directive?

From Dev

How can I pass a scope variable into a directive's `tAttrrs` object?

From Dev

PHP: Can't access $db variable in function's Scope

From Dev

Using a local variable outside of its declaring scope; why does this work?

From Dev

List appears as null if used outside the local variable scope in Java

From Dev

Accessing a variable that's outside of a script

From Dev

Manipulate a local static pointer variable outside the function where it is defined

From Dev

Manipulate a local static pointer variable outside the function where it is defined

From Dev

In functional programming, can a function call another function that was declared outside of it's scope and not passed as a parameter?

From Dev

for loop and iterator variable's scope

From Dev

OOP: Bounds of a variable's scope

From Dev

C++ Local object goes out of scope returning a pointer (memory allocated using new). Memory leak because of this

From Dev

how can i use for loop scope variable form outside for loop?

From Dev

Can't access variable outside the function scope in JS

From Dev

How can I access another computer's terminal outside the local network?

From Dev

Setting pointer out of it's memory range

From Dev

Will Python close an fd if it's out of a local scope?

Related Related

  1. 1

    Pointer can point local variable's memory outside it's scope?

  2. 2

    Can a local variable's memory be accessed outside its scope?

  3. 3

    How to pass variable outside method's scope in react?

  4. 4

    What's the point of the local variable in Eiffel's attached-statement?

  5. 5

    What will a pointer to a object point to outside the object scope in C++

  6. 6

    Can I use values of array declared as local variable outside of its scope?

  7. 7

    Floating point variable's value missing in data memory

  8. 8

    What's the point of the pointer in MASM?

  9. 9

    Save pointer's memory address

  10. 10

    Get the scope's element outside the directive

  11. 11

    Acessing a locally declared struct outside of it's scope

  12. 12

    Reset value(s) of 'this' from outside their scope/function

  13. 13

    How can i use scope's variable in directive?

  14. 14

    How can I pass a scope variable into a directive's `tAttrrs` object?

  15. 15

    PHP: Can't access $db variable in function's Scope

  16. 16

    Using a local variable outside of its declaring scope; why does this work?

  17. 17

    List appears as null if used outside the local variable scope in Java

  18. 18

    Accessing a variable that's outside of a script

  19. 19

    Manipulate a local static pointer variable outside the function where it is defined

  20. 20

    Manipulate a local static pointer variable outside the function where it is defined

  21. 21

    In functional programming, can a function call another function that was declared outside of it's scope and not passed as a parameter?

  22. 22

    for loop and iterator variable's scope

  23. 23

    OOP: Bounds of a variable's scope

  24. 24

    C++ Local object goes out of scope returning a pointer (memory allocated using new). Memory leak because of this

  25. 25

    how can i use for loop scope variable form outside for loop?

  26. 26

    Can't access variable outside the function scope in JS

  27. 27

    How can I access another computer's terminal outside the local network?

  28. 28

    Setting pointer out of it's memory range

  29. 29

    Will Python close an fd if it's out of a local scope?

HotTag

Archive