간단히 말해서 스택 프레임의 개념을 설명하십시오
프로그래밍 언어 디자인에서 콜 스택 에 대한 아이디어를 얻는 것 같습니다 . 그러나 스택 프레임 이 무엇인지에 대한 적절한 설명을 찾을 수 없습니다 (아마도 충분히 열심히 검색하지는 않습니다) .
그래서 누군가에게 몇 마디로 설명해달라고 부탁하고 싶습니다.
스택 프레임은 스택으로 푸시되는 데이터 프레임입니다. 호출 스택의 경우 스택 프레임은 함수 호출 및 해당 인수 데이터를 나타냅니다.
올바르게 기억하면 함수 반환 주소가 먼저 스택으로 푸시되고 지역 변수의 인수와 공간이 푸시됩니다. 아키텍처에 따라 다르지만 함께 "프레임"을 만듭니다. 프로세서는 각 프레임에 몇 바이트가 있는지 알고 있으며 프레임이 스택에서 밀리고 튀어 나오면 그에 따라 스택 포인터를 이동합니다.
편집하다:
높은 수준의 호출 스택과 프로세서의 호출 스택에는 큰 차이가 있습니다.
프로세서의 호출 스택에 대해 이야기 할 때 어셈블리 또는 머신 코드 의 바이트 / 워드 수준 에서 주소와 값을 다루는 것에 대해 이야기하고 있습니다. 고급 언어에 대해 이야기 할 때 "호출 스택"이 있지만 런타임 환경에서 관리하는 디버깅 / 런타임 도구이므로 프로그램에 문제가 발생한 것을 높은 수준으로 기록 할 수 있습니다. 이 수준에서 줄 번호, 메소드 및 클래스 이름과 같은 것이 종종 알려져 있습니다. 프로세서가 코드를 가져올 때까지는 이러한 개념이 전혀 없습니다.
스택을 잘 이해하면 프로그램에서 메모리가 작동하는 방식을 이해하고 프로그램에서 메모리가 작동하는 방식을 이해하면 프로그램에서 함수가 저장되는 방식을 이해하고 프로그램에서 함수가 저장되는 방식을 이해하면 재귀 함수가 작동하는 방식을 이해할 수 있습니다. 재귀 함수의 작동 방식을 이해하면 컴파일러의 작동 방식을 이해하고 컴파일러의 작동 방식을 이해하면 마음이 컴파일러로 작동하고 프로그램을 매우 쉽게 디버깅합니다.
스택 작동 방식을 설명하겠습니다.
먼저 함수가 어떻게 스택에 저장되는지 알아야합니다.
힙 저장소 동적 메모리 할당 값 스택 저장소 자동 할당 및 삭제 값
예를 들어 이해합시다.
def hello(x):
if x==1:
return "op"
else:
u=1
e=12
s=hello(x-1)
e+=1
print(s)
print(x)
u+=1
return e
hello(4)
이제이 프로그램의 일부를 이해하십시오.
이제 스택이 무엇이고 스택 부분이 무엇인지 살펴 보겠습니다.
스택 할당 :
Remember one thing if any function get “return” no matter it have loaded all his local varibles or anything it will immediately return from stack will his stack frame. It means when any recursive function get base condition and we put return after base condition so base condition will not wait to load local variables which are situated in “else” part of program it will immediately return current frame from the stack and now if one frame return next frame is in activation record. See this in practical:
Deallocation of the block:
So now whenever a function found return statement it delete current frame from the stack.
while returning from the stack value will return in reverse order of order in which they allocated in stack.
A quick wrap up. Maybe someone has a better explanation.
A call stack is composed of 1 or many several stack frames. Each stack frame corresponds to a call to a function or procedure which has not yet terminated with a return.
To use a stack frame, a thread keeps two pointers, one is called the Stack Pointer (SP), and the other is called the Frame Pointer (FP). SP always points to the "top" of the stack, and FP always points to the "top" of the frame. Additionally, the thread also maintains a program counter (PC) which points to the next instruction to be executed.
The following are stored on the stack: local variables and temporaries, actual parameters of the current instruction (procedure, function, etc.)
There are different calling conventions regarding the cleaning of the stack.
"A call stack is composed of stack frames..." — Wikipedia
A stack frame is a thing that you put on the stack. They are data structures that contain information about subroutines to call.
Programmers may have questions about stack frames not in a broad term (that it is a singe entity in the stack that serves just one function call and keeps return address, arguments and local variables) but in a narrow sense – when the term stack frames
is mentioned in context of compiler options.
Whether the author of the question has meant it or not, but the concept of a stack frame from the aspect of compiler options is a very important issue, not covered by the other replies here.
For example, Microsoft Visual Studio 2015 C/C++ compiler has the following option related to stack frames
:
- /Oy (Frame-Pointer Omission)
GCC have the following:
- -fomit-frame-pointer (Don't keep the frame pointer in a register for functions that don't need one. This avoids the instructions to save, set up and restore frame pointers; it also makes an extra register available in many functions)
Intel C++ Compiler have the following:
- -fomit-frame-pointer (Determines whether EBP is used as a general-purpose register in optimizations)
which has the following alias:
- /Oy
Delphi has the following command-line option:
- -$W+ (Generate Stack Frames)
In that specific sense, from the compiler’s perspective, a stack frame is just the entry and exit code for the routine, that pushes an anchor to the stack – that can also be used for debugging and for exception handling. Debugging tools may scan the stack data and use these anchors for backtracing, while locating call sites
in the stack, i.e. to display names of the functions in the order they have been called hierarchically. For Intel architecture, it is push ebp; mov ebp, esp
or enter
for entry and mov esp, ebp; pop ebp
or leave
for exit.
That’s why it is very important to understand for a programmer what a stack frame is in when it comes to compiler options – because the compiler can control whether to generate this code or not.
In some cases, the stack frame (entry and exit code for the routine) can be omitted by the compiler, and the variables will directly be accessed via the stack pointer (SP/ESP/RSP) rather than the convenient base pointer (BP/ESP/RSP). Conditions for omission of the stack frame, for example:
- the function is a leaf function (i.e. an end-entity that doesn’t call other functions);
- there are no try/finally or try/except or similar constructs, i.e. no exceptions are used;
- no routines are called with outgoing parameters on the stack;
- the function has no parameters;
- the function has no inline assembly code;
- etc...
Omitting stack frames (entry and exit code for the routine) can make code smaller and faster, but it may also negatively affect the debuggers’ ability to backtrace the data in the stack and to display it to the programmer. These are the compiler options that determine under which conditions a function should have the entry and exit code, for example: (a) always, (b) never, (c) when needed (specifying the conditions).
Stack frame is the packed information related to a function call. This information generally includes arguments passed to th function, local variables and where to return upon terminating. Activation record is another name for a stack frame. The layout of the stack frame is determined in the ABI by the manufacturer and every compiler supporting the ISA must conform to this standard, however layout scheme can be compiler dependent. Generally stack frame size is not limited but there is a concept called "red/protected zone" to allow system calls...etc to execute without interfering with a stack frame.
There is always a SP but on some ABIs (ARM's and PowerPC's for example) FP is optional. Arguments that needed to be placed onto the stack can be offsetted using the SP only. Whether a stack frame is generated for a function call or not depends on the type and number of arguments, local variables and how local variables are accessed generally. On most ISAs, first, registers are used and if there are more arguments than registers dedicated to pass arguments these are placed onto the stack (For example x86 ABI has 6 registers to pass integer arguments). Hence, sometimes, some functions do not need a stack frame to be placed on the stack, just the return address is pushed onto the stack.
참고URL : https://stackoverflow.com/questions/10057443/explain-the-concept-of-a-stack-frame-in-a-nutshell
'Programming' 카테고리의 다른 글
C ++에서 열거 형을 선언 할 때 typedef를 사용하는 이유는 무엇입니까? (0) | 2020.05.24 |
---|---|
배치 파일에 파일이 있는지 확인하는 방법은 무엇입니까? (0) | 2020.05.24 |
AssertjUnit의 문자열에 포함 (0) | 2020.05.24 |
Asp.Net Core에서 동일한 인터페이스의 여러 구현을 등록하는 방법은 무엇입니까? (0) | 2020.05.24 |
종속 DLL이 Visual Studio의 빌드 출력 폴더로 복사되지 않습니다 (0) | 2020.05.24 |