I was trying to access a variable from the previous stack and it gave me the following error in Linux:
.... terminated by signal SIGSEGV (Address boundary error
However in CS61C lecture from 2014, the output was coming as:
3
Something Random
- How did it even work in that machine vs my Linux?
- Why did it print
3
the first time but not the second time? Ifprintf
didn't use that slot for something else, that behavior should have happened the second time as well no?
Below is the code:
#include<stdio.h>
int *ptr() {
int y;
y = 3;
return &y;
}
main() {
int *stackAddr, content;
stackAddr = ptr();
content = *stackAddr;
printf("%d", content);
content = *stackAddr;
printf("%d", content);
}