7 Replies
Because the stack isn't dynamic, it's linear. What makes the heap useful is that you can get a random number of bytes assigned to a specific thing and the location (and size) of said data is returned to reference later. But looking up something in the heap is slow.
The stack, on the other hand, is a concise and packed data structure that only has so much room for stuff. That's how you get the dreaded stack overflow—too many items on the stack and it "overflows". What makes the stack so good is that it's linear space and only the last item is ever in use. Once that last item is done, it's popped off and the next-to-last is ready for use. Repeat until the stack is empty
it's linearim confused more now lol, coz linked lists are also linear but dynamic. mhmm okay
When I say linear I mean linear in memory: the entire item on the stack is in one linear, adjacent region of memory. Heap allocations are all over the place. Linked lists are on the heap, it's one item with a location pointer to the next item. It takes more time to go to that location on the heap than it is to scan the next region of adjacent memory.
one more imp question, what lang is in ur pfp?
i was thinkign hebrew then chinese
Hebrew, yeah
It's my name: Beck
-# (Hebrew being a right-to-left language)
ok thanks
yes, i was thinking linear as in sequential order but its contiguous memory
On of the best ways to visualize stack vs heap that I've seen is to think of the entire program being one large, linear region of memory. The stack is the first X bytes, and the heap is the rest of it. So when you put items on the stack you're going from start to end in the memory allocated for the program. When you store in the heap it's a random location in "the rest" of the program's memory. And that's why the stack is limited in size—once you hit the "end of the stack" you're now in the heap. And that's bad :p