Stack, I think everyone knows. STACK: hardware. A Data Structure is a special linear table that can only be inserted and deleted at one end. The following describes the order of function parameters in C.
People who are persistent in technology, such as me, often want to "know its nature" and "know its nature" for some problems ". C language is profound and profound. Even if I have many years of development experience, I still have many questions. Someone asked me one day what is the order of function parameters in C language in the stack? From right to left, I will answer the question at will. Why is it from right to left? After all, I did not give a reasonable explanation. So I had to do a job, so I had this blog post.
Running result:
X = 100 at [BFE28760]
Y = 200 at [BFE28764]
Z = 300 at [BFE28768]
The C program stack bottom is a high address, and the stack top is a low address. Therefore, the above instance can indicate that the function parameter's import sequence to the stack is indeed from right to left. But why? According to the literature, the parameter stack order is related to the specific compiler implementation. For example, in Pascal, parameters are written from left to right into the stack, and some languages can also be specified through modifiers, such as Visual C ++. either method works. Why does the C language need to be right-to-left?
It is further found that Pascal does not support variable length parameters, but C supports this feature, which makes the order of C function parameters in the stack from right to left. The specific cause is that the number of parameters can be dynamically changed because of the advantage of the C-mode parameter entry sequence (from right to left. Stack heap analysis shows that the top parameters of the stack are pressed at the bottom of the stack from left to right. Unless you know the number of parameters, you cannot obtain the leftmost parameter through the relative displacement of the stack pointer. This makes the number of parameters on the left uncertain, which is the opposite of the number of dynamic parameters.
Therefore, C language function parameters adopt the order of inbound Stack from right to left, mainly because they support variable length parameters. In other words, if this feature is not supported, the C language is exactly the same as Pascal and uses the parameter stack mode from left to right.