The heap vs. the stack
Posted: Wed Sep 21, 2011 2:51 pm
I know what the difference between the heap and the stack is when you code. All the stuff about how a stack is a stack and the heap is for dynamically allocated objects and so on.
For some time now, i have wondered about why the designers of C/C++ (and pretty much every other language too, but often hidden from the user) made this distinction.
All i know about how computers work on the hardware level is from the book called "Code - the hidden language of computer hardware and software"(a book more about hardware than the title suggests). Since all my knowledge about the subject comes from this book, it can be hard to judge how accurate it is, but I'm under the assumption that it gives a very detailed description of a very simple/basic computer. (where very is very relative :P)
But from what i gather, RAM works by you giving it an address through some pins (32 on a 32bit computer i think) and it will give you a value through some other pins. If you want to write to it, you can give it some data through a third set of pins and send it a "write" signal through another pin.
Now, if i where to design a way of setting and getting variable values in a language. I would probably have ended up with something like this. One symbol for denoting if you are talking about the value x or the address x (could for instance be x and &x). And then only letting a single address be on the left side of an = sign. Then in a moment of enlightenment i might have had the idea of putting some alias system on top of it, so i didn't need to bother with all those numbers. (Then i would try to implement it and fail epicly)
I'm no C++ expert, but what I've just described seems to me, to look a little like pointers in C++. A way to throw something on the heap and retrieve it again. I think that functions like malloc and free even could be written on top of that!
I realize that I at this point might be talking complete nonsense......
So, as far is i know, "the stack" is not a hardware thing, it is something some software guy came up with, right? My question is, WHY would he do that? Why bother with pushing and popping and pushing again, when you have random access??
------
The good, but also very annoying, thing about writing long question, is that you sometime end up with the answer. As i was writing this, I began to think that i had actually read something about PUSH and POP assembly instructions in Code, so i looked stack up in the register(in the back of the book, not the CPU register).
It seems, that the reason is functions. When you call a function, everything gets pushed to the stack, and when the function returns everything is pop'ed back. In some way this makes sense to me, but i still wonder where it gets pushed from and pop'ed too? Registers? What if you have more variables on the stack than registers? I think code describes it so that only the registers gets pushed to the stack. This makes sense. But why can you declare variables on the stack yourself then? Why would you do it, except to avoid dealing with pointers (and what comes with them in terms of memory leaks and stuff)? Isn't all that pushing and popping slow? Something tells me that it might have something to do with scope, even though I'm not sure how. If that is the case, is scope really that important?
I will also like to know where the stack is implemented. In hardware (Code talks about push and pop instructions, some tutorials talk about "area in memory"), in the OS, in the application(through the std library) or somewhere else?
Okay, maybe more questions than answers...
P.S. Sorry of all of this is nonsense :S
For some time now, i have wondered about why the designers of C/C++ (and pretty much every other language too, but often hidden from the user) made this distinction.
All i know about how computers work on the hardware level is from the book called "Code - the hidden language of computer hardware and software"(a book more about hardware than the title suggests). Since all my knowledge about the subject comes from this book, it can be hard to judge how accurate it is, but I'm under the assumption that it gives a very detailed description of a very simple/basic computer. (where very is very relative :P)
But from what i gather, RAM works by you giving it an address through some pins (32 on a 32bit computer i think) and it will give you a value through some other pins. If you want to write to it, you can give it some data through a third set of pins and send it a "write" signal through another pin.
Now, if i where to design a way of setting and getting variable values in a language. I would probably have ended up with something like this. One symbol for denoting if you are talking about the value x or the address x (could for instance be x and &x). And then only letting a single address be on the left side of an = sign. Then in a moment of enlightenment i might have had the idea of putting some alias system on top of it, so i didn't need to bother with all those numbers. (Then i would try to implement it and fail epicly)
I'm no C++ expert, but what I've just described seems to me, to look a little like pointers in C++. A way to throw something on the heap and retrieve it again. I think that functions like malloc and free even could be written on top of that!
I realize that I at this point might be talking complete nonsense......
So, as far is i know, "the stack" is not a hardware thing, it is something some software guy came up with, right? My question is, WHY would he do that? Why bother with pushing and popping and pushing again, when you have random access??
------
The good, but also very annoying, thing about writing long question, is that you sometime end up with the answer. As i was writing this, I began to think that i had actually read something about PUSH and POP assembly instructions in Code, so i looked stack up in the register(in the back of the book, not the CPU register).
It seems, that the reason is functions. When you call a function, everything gets pushed to the stack, and when the function returns everything is pop'ed back. In some way this makes sense to me, but i still wonder where it gets pushed from and pop'ed too? Registers? What if you have more variables on the stack than registers? I think code describes it so that only the registers gets pushed to the stack. This makes sense. But why can you declare variables on the stack yourself then? Why would you do it, except to avoid dealing with pointers (and what comes with them in terms of memory leaks and stuff)? Isn't all that pushing and popping slow? Something tells me that it might have something to do with scope, even though I'm not sure how. If that is the case, is scope really that important?
I will also like to know where the stack is implemented. In hardware (Code talks about push and pop instructions, some tutorials talk about "area in memory"), in the OS, in the application(through the std library) or somewhere else?
Okay, maybe more questions than answers...
P.S. Sorry of all of this is nonsense :S