Why is it called stack memory? Does this memory mechanism use the principle of the stack?

Source: Internet
Author: User
Keywords Php c java data structures and algorithms memory

This is the encyclopedia explanation, Java can be replaced by any programming language: C/php/python
It's like we've set a variable at the top of the page.

 $a = 1;(入)$b = 2;(入)print_r($a);print_r($b);

is $ A = 1 the last to be output?
What's going on here? Did I understand the mistake?
Question: Why is it called stack memory? Does this memory mechanism use the principle of the stack?

Reply content:

This is the encyclopedia explanation, Java can be replaced by any programming language: C/php/python
It's like we've set a variable at the top of the page.

 $a = 1;(入)$b = 2;(入)print_r($a);print_r($b);

is $ A = 1 the last to be output?
What's going on here? Did I understand the mistake?
Question: Why is it called stack memory? Does this memory mechanism use the principle of the stack?

Stack memory is generally stored in the function of the call information and variables declared in the function, because the function of the call is recursive, the outer function must be called by the inner layer of the function is loaded and executed, and must wait until the inner layer is called function end before the end, this advanced mechanism is why called stack memory reason.
PS: At compile time, the compiler collects all the defined variables in this function and puts them in front of the function to request memory, so the order in which they go in and out of the stack is not the order you define when you write the program, but the function executes the forward stack and the function executes after the execution is done.

Let's give a practical example:
Let's say one of the calling procedures is this

void a() {    int p = 1;    int q = 2;}void b() {    int x = 3;    int y = 4;        a();        int z = 5;}

The b(); stack memory actually undergoes a change in the process of the call:
( [a] , [b] representative a() and b() basic information, such as program pointers, etc.)

When entering the B function
栈底 < 栈顶(Function information space into the stack)
[b]

Request space for parameters within B function
栈底 < 栈顶(parametric space into the stack)
[b] < x < y < z

Other operations unrelated to the stack, omitted

When entering a function (function information space into the stack)
栈底 < 栈顶
[b] < x < y < z < [a]

Request space for parameters within a function
栈底 < 栈顶(parametric space into the stack)
[b] < x < y < z < [a] < p < q

When a function is completed (parameter space out of stack)
栈底 < 栈顶
[b] < x < y < z < [a]

When exiting a function (function information space out of stack)
栈底 < 栈顶
[b] < x < y < z

When the B function is completed (parameter space out of stack)
栈底 < 栈顶
[b]

When exiting the B function (function information space out of stack)
栈底 < 栈顶

Translation kills people, here the real translation is called "stack frame", Stack frame!

A method in the JVM design is a large stack frame, but there are also several small stack frames within the method, which are not discussed here.

For a temporary variable inside a method, it is allocated in the local variable table of the stack frame (which can be understood as an array). local Variable table

For example, a piece of code will throw p and q two local variables into the local variable table. When a method is written, the size of the local variable table is determined.

void a() {    int p = 1;    int q = 2;}

Next talk about "stack", why is called Stack Frame, in fact, here is a frame,stack is just this Frame modified attributive only. Because the JVM is based on the stack to complete the instruction operation. (Here you can go to the two different VM implementations of Google: based on registers and stack-based), the JVM chose to complete the instruction based on the stack design structure, mainly considering the platform migration factors, because different CPU architecture, the number of registers is indeterminate, Of course you can also play virtual registers, but the overall cost of implementation is complicated.

Therefore, the university's data structure must learn well. Heaps are heaps, stacks are stacks, and there is no separate stack for this data structure description. Stack frames are frames, not stacks!

Maybe you need to take a look at the Csapp or the operating system or the compiler principle (actually Csapp is enough xd) ...
The stack here and the data structure of the stack is just coincidence name ...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.