How to determine if a heap or stack is allocated?

I wonder if there is a way to find out if a variable stack or heap is selected.

Consider this:

struct SomeStruct;

fn main() {
    let some_thing = Box::new(SomeStruct);
    println!("{:p}", some_thing);
    foo(&*some_thing);
}

fn foo (bar: &SomeStruct) {
    println!("{:p}", bar);
}

prints

0x1
0x1

And then

struct SomeStruct;

fn main() {
    let some_thing = &SomeStruct;
    println!("{:p}", some_thing);
    foo(some_thing);
}

fn foo (bar: &SomeStruct) {
    println!("{:p}", bar);
}

prints

0x10694dcc0
0x10694dcc0

I see that the memory address is much shorter for a dedicated heap version, but I don't know if this is a reliable way to tell the difference. I wonder if there is something likestd::foo::is_heap_allocated()

+4
source share
1 answer

- POSIX, sbrk() 0 , . , , , , . , , , , , , , , . , rbp x86_64, . , , , , rsp.

, end() end. , end(end), sbrk(0).

+4

All Articles