Would that be wrong? And if so, why? The output is 2500

The result of the code is below 2500. It contains pointers. Can someone give a proper explanation for this? Why is he printing 2500? Does this happen through a pointer declaration, or is there another reason?

#include <stdio.h> /* Two functions include and they are operated by main function */ int *f(int x) { /* Creates an variable */ int p; p = x; return &p; } /* Here the initialization of the function g */ int *g(int x) { /* Creates an variable */ int y; y = x; return &y; } /* This creates two pointers called x and y */ int main() { int *x, *y; /* Here call the functions f and g */ x = f(100); /* Here call the function g */ y = g(2500); /* How does it print 2500? */ /* print the value of x */ printf("%d \n", *x); return 0; } 
+5
source share
3 answers

The reason you get weird output is because the behavior is undefined. You are returning the address of an automatic local variable that will no longer exist as soon as the function reaches its end.

Although an explanation of the output can be given in terms of the frame of the function call stack. Since the last call to the function g , and the argument passed to it 2500 , the parameter x function g is allocated on the stack, and 2500 is 2500 onto the stack. When this function returns, this value slipped out of the stack (although the stock frame for g not valid after returning to the caller), and it can return this 2500 from its stack frame.

+8
source

In both of your functions, you are trying to return the address of a local variable to a function. As soon as the function finishes execution and the control returns to the caller, the return address will become invalid (i.e., the Variables are out of scope), and any attempt to use the return value will cause undefined .

If you need to return the address from a function and use it in the caller, you will need a pointer to which memory is allocated through dynamic memory allocation functions, for example malloc() and family.

+5
source

I think that what is happening can be explained using the stack. When the function f is called, the stacks behave as follows. "ret" is the return address.

 2| | 2| | 2| | 2| p | 1| | -> 1| | -> 1|ret| -> 1|ret| 0| | 0|100| 0|100| 0|100| 

Now the stack pointer is 2, and this address (address p) is returned and assigned * x in main. When it returns from f, all stack values ​​are unloaded, but in fact what happens is simply decreasing the value of the stack pointer. Therefore, the values ​​that were pressed still exist. The g function repeats, and the stack looks like this.

 2| p | 2| p | 2| p | 2| y | 1|ret | -> 1|ret | -> 1|ret | -> 1|ret | 0|100 | 0|2500| 0|2500| 0|2500| 

Same as function f, address p is returned and assigned * y in main. But note that this is also the same address as p, 2. Therefore, * x and * y are basically both pointing to the same place. The g function sets the values ​​of this address to 2500, so both * x and * y print 2500.

Hope this helps. :)

+2
source

Source: https://habr.com/ru/post/1239258/


All Articles