Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to tell if something is heap or stack allocated?

I wonder if there's a way to figure out if a variable is stack or heap allocated.

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 can see that the memory address is much shorter for the heap allocated version but I don't know if that's an reliable way to tell the difference. I wonder if there's something like std::foo::is_heap_allocated()

like image 907
Christoph Avatar asked May 10 '15 22:05

Christoph


People also ask

How do you know if an object is in stack or heap?

If the object was created on the stack it must live somewhere in the direction of the outer functions stack variables. The heap usually grows from the other side, so that stack and heap would meet somewhere in the middle.

What is the difference between stack and heap allocation?

Key Differences Between Stack and Heap AllocationsIn a stack, the allocation and de-allocation are automatically done by the compiler whereas in heap, it needs to be done by the programmer manually. Handling of Heap frame is costlier than the handling of the stack frame.

Where are stack and heap allocated memory?

Stack always stored blocks in LIFO order whereas heap memory used dynamic allocation for allocating and deallocating memory blocks. Memory allocated to the heap lives until one of the following events occurs : Program terminated.

Is everything allocated on the heap in Java?

All objects, including their individual attributes, are stored on the heap. All local variables, and their arguments, are stored on the stack because they contain primitive values or references.


1 Answers

If you're on some POSIX system, you can probably use the sbrk() system call with an argument of 0 to determine the current location of the program break, which is the current limit of the heap. If the address of a given value is less than this address but greater than the start of the heap then it's on the heap. I don't know how you'd check if it's on the stack though, which isn't necessarily automatically the alternative of not being on the heap, since it can also be statically initialized or uninitialized data, though that would probably be obvious to you upon inspection of the code. You can probably use the rbp register on an x86_64 architecture, which should point to the beginning of the current stack frame. That's if you want to check if it's on the current stack frame, or if you want to check if it's anywhere on the stack you can probably use rsp.

I think you can get the start of the heap with the end() system call using the end argument. So the lower bound of the heap would be the result of end(end) and the upper bound would be sbrk(0).

like image 193
Jorge Israel Peña Avatar answered Oct 06 '22 09:10

Jorge Israel Peña