Stack frame---Changes to data in program run

Source: Internet
Author: User

First, stack

Stacks exist as a special data structure ("back-in-first-out" storage), which is a special linear table that can only be inserted and deleted at one end.

Most of the program implementations on the CPU use stacks to support function invocation operations. Stacks are used to pass function parameters, store return information, temporarily save the original value of the Register for replies, and store local data.

Stack has a lot of its own features, it has a memory function, the stack of INSERT and delete operations, do not need to change the stack bottom pointer, and the stack is from the high address to the low address extension. Each invocation of each function has its own stand-alone stack frame, which maintains all the necessary information. So the stack function is to keep the stack frame active record (i.e. function call).

Stack relative to the entire system, the call stack is relative to a process, the stack frame is relative to a function, the call stack is the stack space in use, consisting of multiple nested call functions used by the stack frame.

If the C-language address character ' & ' is applied to a local variable, the stack needs to allocate a space for the variable to generate an address value that is the variable's address value.

650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M01/7E/69/wKioL1b-dCzRxcSZAAC2zN_ZUDk527.jpg "title=" Memory 111.jpg "style=" Float:none; "alt=" wkiol1b-dczrxcszaac2zn_zudk527.jpg "/>

Second, stack frame

The stack frame represents the function call record of the program, and the stack frame is recorded on the stack, and it is obvious that the stack holds n stack frame entities, it can be said that the stack frame is divided into n record blocks, but these record block size is not fixed, because the stack frame not only save such as: function, parameter, Returns information such as the stack-bottom pointer for the address and the previous stack frame, as well as saving the automatic variables inside the function (even the dynamic allocation of memory, which can be implemented by the ALLOCA function, but not in some systems), so not all stack frames are the same size.

The stack part used by a single function call operation is called the stack frame result. Each end of the stack frame structure is specified by two pointers. The register EBP is typically used as a pointer to a stack frame, and ESP as a pointer to a stack. ESP with data in the stack and out of the stack. Therefore, access to most of the data in a function is achieved through the frame-frame pointer ebp.

650) this.width=650; "src="/e/u261/themes/default/images/spacer.gif "style=" Background:url ("/e/u261/lang/zh-cn/ Images/localimage.png ") no-repeat center;border:1px solid #ddd;" alt= "Spacer.gif"/> ebp Point is currently at the bottom of the top stack frame of the system stack, Rather than the bottom of the system stack; the top of the stack frame referred to by ESP is the same position as the top of the system stack.

650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M00/7E/6D/wKiom1b-cMChV0PmAAD2mFtRVSM399.jpg "title=" stack frame structure. jpg "style=" float:none; "alt=" wkiom1b-cmchv0pmaad2mftrvsm399.jpg "/>

Cases:

Program code:

#include <stdio.h> #include <stdlib.h>int stack_test (int a,int b) {printf ("Before write:0x%x\n", b);    int *p=&a;    p++;    *P=0XDDDD;    printf ("After write:0x%x\n", b);    int C=0XCCCC; return c;}    int main () {int a=0xaaaa;    int b=0xbbbb;    int Ret=stack_test (A, b);    printf ("You should run here\n"); return 0;}

Operation Result:

650) this.width=650; "src="/e/u261/themes/default/images/spacer.gif "style=" Background:url ("/e/u261/lang/zh-cn/ Images/localimage.png ") no-repeat center;border:1px solid #ddd;" alt= "Spacer.gif"/>650 "this.width=650;" src= "http ://s3.51cto.com/wyfs02/m02/7e/69/wkiol1b-cwecnq4saadx9mqncsk179.jpg "title=" in front of the comment result 1.jpg "style=" Float:none; "alt= "Wkiol1b-cwecnq4saadx9mqncsk179.jpg"/>

Results Analysis:

Changes in the data during program operation:

650) this.width=650; "src=" http://s1.51cto.com/wyfs02/M00/7E/6D/wKiom1b-dQDTjNY4AAEA_n2h2JQ172.jpg "title=" stack 1-3.jpg "alt=" Wkiom1b-dqdtjny4aaea_n2h2jq172.jpg "/>


Before the program runs,&b=0xbbbb;

When the program is running, the system is reassigned to a temporary space. *p=&a, that is, the temporary copy of A to the new memory space, p++,*p move up one bit, and at this time *p address is given 0XDDDD.

And the stack with "in-First out" storage, that is, at this time *p is the bottom of the new stack frame, first step out, thus &b=*p=0xdddd.

Cases:

The procedure is as follows;

#include <stdio.h> #include <stdlib.h>int bug () {System ("reboot"); Exit (0);}    int stack_test (int a,int b) {int *p=&a;    p--;    *p=bug ();    int C=0XCCCC; return c;}    int main () {int a=0xaaaa;    int b=0xbbbb;    int Ret=stack_test (A, b);    printf ("You should run here\n"); return 0;}

Operation Result:

System restart

Results Analysis:

Similar to the previous example, when a program runs to a bug (), the program's internal function system ("reboot") commands the system to restart, and then exit (0) ends the program normally.


This article is from the "Flower Open Shore" blog, please be sure to keep this source http://zxtong.blog.51cto.com/10697148/1759378

Stack frame---Changes to data in program run

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.