Home > Enterprise >  Is stack frame random access?
Is stack frame random access?

Time:11-27

I know that when a function gets called, a stack frame is created for it which contains(local variables,return address,frame pointer...) and pushed on to the program stack. We are able to use the passed aurguments randomly.

Void func(int a,int b,int c){
//a,b,c
//c,b,a
//a,c,b
}

In the above function the arguments can be use randomly, I know that the stack is LIFO(last in first out), for now I just want to know, is the stack frame random access? Because we are able to access the variables (local variables) randomly.

CodePudding user response:

On an x86_64 machine, your values for a, b, and c would be stored in the lower-32 bits of the %rdi, %rsi, and %rdx registers, which correspond to some of the registers in your hardware.

The stack itself is just a location in memory, typically in DRAM, that you can access as you please, specifically allocated in "frames," based on your function calls. Your function that you just called would have its own "frame."

CodePudding user response:

Yes, on all platforms I'm aware of that use a stack all RAM is random access (that's what the 'RA' stands for after all).

The stack is just a convention for managing ownership and organization of your program's memory so that different function calls don't try to use memory that other function calls are still using. Each function call pushes a stack frame onto the top of the stack to indicate what memory it needs to use. It can randomly access any of that memory (or any other memory; the CPU doesn't prevent functions from accessing other functions' stack frames) as it pleases. The stack frame is just a way of telling other function calls that the memory is in use.

  • Related