标签云

微信群

扫码加入我们

WeChat QR Code

Possible Duplicate:
What and where are the stack and heap

I am programming in C++ and I am always wondering what exactly is stack memory vs heap memory. All I know is when I call new, I would get memory from heap. If if create local variables, I would get memory from stack. After some research on internet, the most common answer is stack memory is temporary and heap memory is permanent.

Is stack and heap memory model a concept of operating system or computer architecture? So some of it might not follow stack and heap memory model or all of them follow it?

Stack and heap memory is the abstraction over the memory model of the virtual memory ( which might swap memory between disk and RAM). So both stack and heap memory physically might be RAM or the disk? Then what is the reason where heap allocation seems to be slower than the stack counterpart?

Also, the main program would be run in the stack or a heap?

Also, what would happen if a process run out of the stack memory or heap memory allocated?

Thanks


meagar This does raise some questions not mentioned in the question you linked to.

2018年08月15日25分14秒

This is wrong in so many ways. There is typically no difference at all between "stack memory" and "heap memory", on any architecture I'm aware of. Both can be accessed by stack pointer, index registers, or whatever. Both the stack pointer and index registers can be changed arbitrarily to access any memory available to the process.

2018年08月16日25分14秒

unapersson: The RAM is the RAM, and the SP can be change and assigned of course. "Stack Memory" is more a term of intent or role - not necessarily of a specialize piece of hardware. BTW, in the case of the 6502, your stack was indeed stuck at Page 1 (bytes 256 thru 511).

2018年08月16日25分14秒

rskar Yes indeed. I wrote an implementation of the KERMIT protocol, with full VT100 terminal emulation on the BBC Micro in 6502 assembler (not a happy experience for a Z80 programmer) back in 1984, so I do know of what I speak here.

2018年08月16日25分14秒

rskar For most architectures, there is no difference between stack memory and ordinary memory. Even on the 6502 you could (and sometimes had to) implement your own stack in "ordinary" memory - you just could not use it via call and return opcodes. Even on the 6502 there was nothing special about stack memory, only about how it was accessed (via the 6502 stack page) You seem to be suggesting that there is actually a physical difference between stack and other types of memory.

2018年08月16日25分14秒

unapersson: My apologies - I never meant to suggest that there is actually a physical difference between stack and other types of memory.

2018年08月16日25分14秒

"Stack memory almost always must be manually freed." You meant heap memory. Also, it's worth mentioning that heap memory is "global", when you run out of heap other applications will also run out of heap. The stack though, has local scope.

2018年08月16日25分14秒

"Make a system backup" before trying to fill the heap? What platform are you on, MS-DOS?

2018年08月16日25分14秒

larsman No, I've had this crap out expensively on Unix systems where user limits were not set. And it will probably cause problems even on "modern" operating systems - try it (with my proviso).

2018年08月16日25分14秒

unapersson: I've run out of memory on Unix and Linux systems before. I never experienced any great problems; usually just a long slow-down until everything was back in order, sometimes the necessity to reboot.

2018年08月16日25分14秒

larsman But did you remove the limits on user processes? And have you tried it on, say, Windows? And most for most of us Unix folks, the "necessity to reboot" is the next step to getting the sack.

2018年08月16日25分14秒

unapersson: all systems (Linux, Mac OS X, OpenBSD) had default limits. And no, I never try anything heavy on Windows, I hate cleaning up broken glass ;)

2018年08月16日25分14秒