I test the code as following
fn test_stack_array() -> [u8; 512*1024] {
let stack_top = 1;
let array = [0; 512*1024];
let stack_bottom = 1;
println!("addr of (top, bottom) of stack is (0x{:x}, 0x{:x}), addr of (array[0], array[-1]) is (0x{:x}, 0x{:x})",
&stack_top as *const i32 as usize, &stack_bottom as *const i32 as usize,
&array[0] as *const u8 as usize, &array[512*1024-1] as *const u8 as usize);
array
}
....
let a = test_stack_array();
println!("access array a[]:{}, addr of array out of function is : 0x{:x}", a[10], &a as *const u8 as usize);
.....
the result:
addr of (top, bottom) of stack is (0x19f610, 0x19f614), addr of (array[0], array[-1]) is (0x19f760, 0x21f75f)
access array a[]:0, addr of array out of function is : 0x19f760
returns the array allocated in function(at the stack space) , which means that space will not be freed. so does it mean the stack will exist many leaks and be managed like in heap? I think managing stack is more efficient than managing heap, is it correct?
Please help, thanks