3 ms·
I was actually asked this problem on a job interview (not Google) and gave the answer your boss pointed out, but the interviewer then pointed out that local var
by tlrobinson 18y ago
I was actually asked this problem on a job interview (not Google) and gave the answer your boss pointed out, but the interviewer then pointed out that local variables aren't necessarily laid out in memory in the order they're declared.
The function call is the only reliable way I can think of. Any others?
- utnick 18y agowould this work? in main: int x; int *p = &x; start writing random data to p, p-1, p-2, etc etc return; if nothing bad happens the stack grows up?
- aston 18y agoWhat's your definition of "nothing bad happens"? And how do you recover if it does happen?
- huhtenberg 18y ago> Any others? alloca() Also, for the two-variable approach something like this should force the compiler to allocate variables in a required order: int main() { int foo; { int bar; ... } ... }