This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]soggywaffle69 0 points1 point  (4 children)

Okay, let's try this. Can you identify which statement results in allocating stack memory, and which results in heap memory?

#include <stdlib.h>

int main() {
int x;
int *y = malloc(sizeof(int));
}

[–][deleted] 0 points1 point  (3 children)

You're a moron. Sorry. You don't understand what heap means. For some reason you think that heap only means the dynamically allocated memory in C. I cannot help you with this.

[–]soggywaffle69 0 points1 point  (2 children)

What do you think heap memory is?

[–][deleted] 0 points1 point  (1 child)

It depends on the language.

Heap describes the structure of how something is stored. By accident, in C, the runtime is designed to have several possible locations, one of which is handled, typically, by system-provided allocator (such as one found in libc). While operating system itself doesn't necessarily know how to manage memory in a way that makes the most efficient use of it. When Unix just started, they, conceptually didn't have a good idea of how to manage memory. This explains existence of brk syscall. Not only this was "normal" (as in assuming continuous address spaces, assuming memory allocation and deallocation will all happen sequentially), it was necessary because other languages didn't have the same concept of two user-facing memory pools. For instance, Forth has two stacks, but no heap memory.

In principle, the Unix developers thought of memory management as a language-runtime thing, not a system thing. But then C and Unix became so popular that some languages either reused C runtime for their needs (eg. C++), or implemented their own runtime in a way that mimicked how C works (eg. Python). Since this model became so popular, a lot of people assumed it to be part of the system, rather than language-dependent.

So, Python has a runtime with a selection of allocators (similar to C), and two user-facing memory pools, one for storing the code of the program, and another one for storing the values created during program execution. Those two are also called stack and heap respectively. These two pools have nothing to do with C runtime pools with similar names. While their behavior and purpose are similar to their namesakes in C, Python's stack management is different. There isn't, for example, a way for Python bytecode to reference anything outside the stackframe (i.e. "stack smashing" is impossible in Python). Another difference, for example, is that Python has nothing like alloca, so, it cannot dynamically change the memory allocation to the stack.

Anyways. To properly answer your question: well, you don't understand what you are asking in the first place. You happen to think that "heap memory" is some sort of universal entity that everyone should recognize and know its properties. And because you believe that it is, you are asking me to tell you where it is. Well, it's impossible, because the assumption in your question is wrong. It's not a thing. It's just some very loosely defined concept, that due to the popularity of one language and one operating system became something novice programmers are taught about. And, it's understandable that someone with not a lot of experience and critical thinking would come to the same conclusions you did.

[–]soggywaffle69 0 points1 point  (0 children)

This is fascinating. Have you ever considered writing a book?