Data Structure _ Stack

Source: Internet
Author: User

---- Notes about Stack

A stack is a simple data structure. It is a linear table that can be inserted or deleted only at one end. one end of the insert or delete operation is the stack top, and the other end is called the stack bottom. stack insertion and deletion operations are called inbound and outbound stacks. there is a set of CPU commands to implement stack access to the memory of the process. among them, the pop command implements the outbound operation, and the push command implements the inbound operation. the ESP register of the CPU stores the top pointer of the current thread, and the EBP register stores the bottom pointer of the current thread. the EIP register of the CPU stores the memory address of the next CPU command. after the CPU executes the current command, it reads the memory address of the next command from the EIP register and continues to execute the command.

We all know that high-level languages can access data in the memory through variable names. How are these variables stored in the memory? How does a program use these variables?

First, let's take a look at how C language variables are distributed in memory. C language has global variables, local variables, static variables and register variables. Each variable has a different allocation method. See the following:

 

The output result is the memory address of the variable. it can be found that these variables are continuously distributed in the memory, but the local variables and the memory address allocated by the global variables are far behind, while the memory allocated by the global variables and static variables is continuous. this is because local variables and global variables are allocated to different types of memory areas. the memory space of a process can be logically divided into three parts: Code zone, static data zone, and dynamic data zone. the Dynamic Data zone is generally a "stack ". stack and stack are two different dynamic data zones. stack is a linear structure, and stack is a chain structure. each thread of a process has a private stack. Therefore, although the Code of each thread is the same, the data of local variables does not interfere with each other.

A stack can be described through the base address and stack top address. global variables and static variables are allocated in the static data zone. local variables are allocated in the dynamic data zone, that is, in the stack. the program accesses local variables through the base address and offset of the stack.

Stack is an advanced and post-release data structure. The top address of the stack is always less than or equal to the base address of the stack. we can first take a look at the function call process, so that we can have a deeper understanding of the role of the stack in the program. different Languages have different function calling rules. These factors include the parameter loading rule and the stack balance. the Calling rules of Windows APIs are different from those of ansi c. the former is adjusted by the called function, and the latter is adjusted by the caller. the two are differentiated by the prefix "_ stdcall" and "_ cdecl. see the following code:

 

This is what the stack looks like during the function call process. first, the three parameters are pushed to the stack in order from right to left. Press "param3" first, press 2, and then press 1. press the return address (RET) of the function, jump to the function address, and execute. step 3: subtract a number from the top stack (ESP) to allocate memory space for local variables. In the above example, the value is 12 bytes (esp = ESP-3*4, each int variable occupies 4 bytes ). then initialize the memory space of the local variable. because the "_ stdcall" call is adjusted by the called function, the stack must be restored before the function is returned. The memory occupied by local variables (esp = ESP + 3*4) must be recycled first ), then retrieve the return address, fill in the EIP register, reclaim the memory occupied by the previously pushed parameters, and continue executing the caller's code.

 

Dynamic Data in windows can be stored in the stack and heap. all C ++ users know that C ++ can use the new keyword to dynamically allocate memory. see the following C ++ code:

We can find that the memory allocated with the new keyword is neither in the stack nor in the static data zone.

**************************************** **************************************** ********************************

Linear Structure:

For a Data Structure course, a linear structure is a set of n elements in order. It has four basic features:

1. A unique "first element" must exist in the collection ";

2. A unique "last element" must exist in the collection ";

3. Except the last element, other data elements have unique "successors ";

4. Apart from the first element, other data elements have a unique "precursor ".

In a data structure, a linear structure refers to a data structure with a "one-to-one" linear relationship between data elements.

For example, (A1, A2, A3,..., an), A1 is the first element, and an is the last element. This set is a set of extremely linear structures.

The logical feature of a non-linear structure is that a node element may correspond to multiple direct prefixes and multiple rear drives.

Common linear structures include linear tables, stacks, queues, dual queues, arrays, and strings.

Common Non-linear structures include: trees (Binary Trees, etc.) and graphs (networks, etc ).

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.