The Three Kingdoms in the 39th course program

Source: Internet
Author: User

1. Stacks in the program

1.1 Introduction to Stacks

(1) One of the most important concepts in modern computer programs in stacks

(2) The stack is used in the program to maintain the function call context

(3) The parameters and Local variables in the function are stored on the stack

(4) The stack holds the maintenance information required for a function call

1.2 function Call Procedure

(1) Each function call corresponds to the activity record on a stack, the activity record of the calling function is in the middle of the stack, and the activity record of the called function is at the top of the stack .

(2) Change of function call stack: with int main () {f ();} →f () {} For example

1.3 data on the function call stack

(1) When the function is called, the corresponding stack space is dedicated before the function returns .

(2) After the function call is finished, the stack space is freed and the data is no longer valid

"Programming experiment" pointers to stack data

#include <stdio.h>int*g () {inta[Ten] = {0 }; returnA//returns an array in the stack}voidf () {inti =0; intarray[Ten] = {0,1,2,3,4,5,6,7,8,9 }; int* pointer = g ();//pointer points to an array in the G function stack    /*//When the G function returns, the array in the stack is immediately saved to the array//But if there are other functions called, then the array data in the stack pointed to by pointer will be invalid!    for (int i = 0; i < 10;i++) {Array[i] = Pointer[i];    } for (i = 0; i < 10;i++) {printf ("array[%d] =%d\n", I, array[i]); }    */}intMain () {f (); return 0;}

2. Heap in the program

(1) heap is a reserved memory space in the program, which can be freely used by the program

(2) The memory in the heap that is requested by the program will be active until it is actively released .

(3) Why does the stack need to be stacked ? (the data on the stack is freed after the function is returned and cannot be passed outside the function, such as a local array)

(4) The heap space is obtained from the call of the library function in C language: malloc and free

(5) How the system manages the heap space (idle list method, bitmap method, Object pool method, etc.)

3. static Storage area in the program

(1) The static storage area allocates space as the program runs

(2) The life cycle of the static storage area until the end of the program run

(3) The size of the static storage area is determined during the compilation period of the program.

(4) Static storage is mainly used to save global variables and static local variables

(5) Static store information is eventually saved to the executable program

Validation of the "programming experiment" static storage area

#include <stdio.h>intG_v =1;//Static ZoneStatic intG_vs;//static zone, only this file is visiblevoidf () {Static intG_V1 =3;//local static variable--static zoneprintf"G_V1 =%p\n", &g_v1);}intMain () {printf ("G_v =%p\n", &g_v);//G_v, G_vs, g_v1 address compare adjacentprintf"G_vs =%p\n", &G_vs);    f (); return 0;}

4. Summary: Three basic data Areas

(1) The stack area is used primarily for function calls

(2) The heap area is mainly used for dynamic application and return of memory.

(3) static storage for saving global variables and static variables

The Three Kingdoms in the 39th course program

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.