I wrote the following code
#include "stdio.h"
struct node {
int x;
int y;
};
typedef struct node Node;
Node * f() {
Node node = {1, 2};
return &node;
}
int main() {
Node *node = f();
printf("(%d, %d)", node->x, node->y);
printf("(%d, %d)", node->x, node->y);
}
During compilation, I get warning: address of stack memory associated with local variable 'node' returned
warning.
However, the printf
- prints the correct result the first time
- prints the wrong result the second time
This brings me to my question: when exactly is the stack of the call to f()
cleared??