Suppose the array x was destroyed. What output did you expect? 0? 0xdeadbeef?
I was expecting the compiler to throw an error or something.
This time, I ran this modified code:
Code:
#include <stdio.h>
#include <stdlib.h>
int* testfunc()
{
int x[10];
x[0]=10;
return x;
}
void testfunc2()
{
int t[10];
t[0]=4;
return ;
}
int main()
{
int *x=testfunc();
testfunc2();
printf("%i",*x);
}
And the output was ...*drum rolls*.... 4!
It seems that this is indeed the result of undefined behavior. I assume that a function's local variables remain in the stack until they are overridden by another function call (please correct me if i'm wrong).
Thank you very much for your assistance