I have a small C code which demonstrate Runtime Stack functionality by modifying data at a stack address.
#include <stdio.h>
int * fun() {
int a = 10;
return &a;
}
int * fun2() {
int b = 20;
return &b;
}
int main () {
int *a = fun();
int *b = fun2();
printf("a:%d b:%d\n", *a, *b);
return 0;
}
Output of this is : a:20 b:20 which shows 'b' in 'fun2' used the same stack address as 'a' in 'fun'.
i want to test this in Rust too. What would be the simplest way to do this?
I tried Borrowing but compiler says "No value to borrow".
If you want to have the same undefined behavior as in C, you can do (luckily, this requires
unsafe):And this is UB exactly as with the C version. It prints
a: 21885 b: 21885on the playground on debug mode, anda: -482758656 b: 32767on release mode. Yours will be different.If you also want the same result as the C version, you can use
printf(). Be careful with the code you use, changes can cause it to no longer "work" as it will use a different stack layout. Of course, this is very fragile:It prints
a: 20 b: 20on debug mode, anda: 1 b: 1on release (and yours may be different).