stack space allocation

Memory is divided into heap and stack. The heap is dynamically allocated. New, malloc, etc. are all allocated memory on the heap, which requires manual recycling or platform recycling. The stack is used to allocate local variables, method parameters, and return addresses, which are automatically recycled after the method runs.
For statically typed languages, you can know what local variables and parameters this method has at compile time, and the size of the memory occupied is also determined, so it can be allocated directly on the stack, which is called static allocation of the stack. For dynamic data types such as python and javascript (python and javascript are all object languages), it is impossible to know how many bytes local variables and parameters occupy before the program runs, so local variables and parameters may be allocated on the heap (Not sure about this, guesswork).

For static allocation of the stack, the following code (java):
void foo(){
 int a=0;
 int b=1;
 print(a);
}


When pushing the stack, a precedes b, so b is the top of the stack. At this time, if you want to read a according to the data structure, you can only do it by popping the stack. At this time, you can know the pointer and offset of the top of the stack. After calculating the address of a, you can directly read a through the address. It does not mean that you need to pop the stack. Refer to: http://blog.csdn.net/chenlycly/article/details/37912683
In addition, Q group Daniel recommends reading a book Book: CSAPP (Computer System: A Programmer Perspective).

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326853639&siteId=291194637