Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are different storages available/used to/by a program during execution time?

Typically when you run any program, during execution time what are different storages available to it and what are they used for? I understand stack and heap. Also, I know that value types go in stack whereas ref types go in heap. But, I have also come across terms like program counter, instruction pointer.

What do they mean?

BOUNTY: There are some really good answers below. I am looking for something more detailed. Something, which will not compel me to read a few chapters from a COA book. Specific blogs/videos/explanation appreaciated.

like image 383
Sandbox Avatar asked Dec 19 '25 00:12

Sandbox


1 Answers

The main types of memory in any high-level language are the two you identified already: stack and heap.

There's no limit to what you can put in one or the other; the difference is how and when that memory gets allocated. Stack space is allocated when a function is called, and all at once, so you cannot allocate any new stack space from inside the function, and once the function returns that memory is deallocated. Heap space, on the other hand, you allocate yourself whenever you want and in whatever quantities you want.

There are certainly other chunks of memory that are used internally that you wouldn't normally touch. "Program counter" and "instruction pointer," for example, are both the same thing: a single word of memory that keeps track of which instruction in your program is the next to execute. Each time the CPU executes an instruction, the program counter will move on to the next one.

Once you make a function call, the program counter gets stored on the stack alongside your own local variables, so that when the called function returns the calling function will know where it "left off."

like image 108
VoteyDisciple Avatar answered Dec 24 '25 10:12

VoteyDisciple



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!