How do Haskell compilers decide whether to allocate on the heap or the stack?

When you call a function like this f 42 (g x y) then the runtime behaviour is something like the following: p1 = malloc(2 * sizeof(Word)) p1[0] = &Tag_for_Int p1[1] = 42 p2 = malloc(3 * sizeof(Word)) p2[0] = &Code_for_g_x_y p2[1] = x p2[2] = y f(p1, p2) That is, arguments are usually passed as … Read more

Print out value of stack pointer

One trick, which is not portable or really even guaranteed to work, is to simple print out the address of a local as a pointer. void print_stack_pointer() { void* p = NULL; printf(“%p”, (void*)&p); } This will essentially print out the address of p which is a good approximation of the current stack pointer

Fields of class, are they stored in the stack or heap?

as I understand, int is value type and therefore lives in the stack Your understanding is incorrect. Value types are called “value types” because they are copied by value. Reference types are called “reference types” because they are copied by reference. It is not at all true that “value types always live on the stack”. … Read more

Are global variables in C++ stored on the stack, heap or neither of them?

Here is what the book says on page 205: If you’re familiar with operating system architecture, you might be interested to know that local variables and function arguments are stored on the stack, while global and static variables are stored on the heap. This is definitely an error in the book. First, one should discuss … Read more

Class members and explicit stack/heap allocation

I think that you are confusing “stack/heap allocation” and “automatic variable”. Automatic variables are automatically destroyed when going out of context. Stack allocation is the fact that the memory is allocated on the execution stack. And variable allocated on the stack are automatic variables. Also, members are automatic variables whose destructors get called when its … Read more

Heap vs Stack vs Perm Space

Simply Heap space: All live objects are allocated here. Stack space: Stores references to the object for variable in method call or variable instantiation. Perm space: Stores loaded classes information For example: Student std = new Student(); after executing the line above memory status will be like this. Heap: stores “new Student()” Stack: stores information … Read more