For the analysis of the running stack of the main function, the main function stack

Source: Internet
Author: User

For the analysis of the running stack of the main function, the main function stack
Analysis of the running stack of the main Function

Here, we need to note that the runtime environment is 64-bits Ubuntu. the compiler is gcc.



Code for testing:

int main(int argc,char* argv[]){int array[10];array[0] = 10; array[9] = 9; return 0;}


Use GDB to debug this code. Let's observe and analyze the stack of the main function.





Analysis & argc and & argv can be used to know the current usage of the main function stack.


64 bits machines. The pointer length is 64 bits, that is, 8 bytes.

For the sake of byte, the running status of the stack is as follows:

 



Tip: If % rbp is damaged, the call main cannot be returned, and the core dump

This blog is intended to pave the way for CSAPP lab0 ~

Http://blog.csdn.net/cinmyheart/article/details/39138915







C ++ just runs to main and Stack overflow

There may be an endless loop. You have been compressing data in the stack, and the stack size is very limited.

The following break may not be executed at all.
If (temp1 = standard) break;
You need to change it.
If (temp1 = standard)
{
Cout <"braak ";
Break;
}
You can see whether it is overflow before or after interruption.
Now we can see that the breakpoint is set elsewhere.

Similarly, by following this method, you will soon find the problem.

Briefly describe the role of the main function

[Edit this section] main Function Overview
In daily life, to complete a complex function, we are always used to breaking down the "big function" into multiple "small functions" for implementation. In the world of C ++ programs, "function" can be called "function". Therefore, "function" is actually a code that implements a certain function and can be called by other code.
A program, whether complex or simple, is generally a "function". This function is called "main function", that is, "main function ". For example, if you have a "Cooking" program, the "Cooking" process is the "main function ". In the main function, you may also need to call subfunctions such as "shopping, cutting, and cooking.
[Edit this section] main Function Parameters
The biggest feature of a C program is that all programs are assembled using functions. Main () is called the main function and is
The entry for sequential operation. Other functions are divided into two types: the main () function or other common functions.
If there is a parameter function, the parameter is passed during the call.
Main ()
{
...
Y1 = f1 (x1, x2 );
...
}
F1 (int a, int B)
{
....
Y2 = f2 (x3, x4 );
....
}
F2 (int m, int n)
{
....
.....
}
In the previous course, the main () function is always treated as the main function, that is, the main () function is allowed to be called.
Other functions and pass parameters. In fact, the main () function can be neither a Parameter Function nor a function with parameters. For
Parameters must be passed to them. However, no other function can call the main () function. Of course, the same is true.
It cannot be passed to the main () function. It can only be passed outside the program. How can this problem be solved?
Let's take a look at the form of the main () function with parameters:
Main (argc, argv)
Int argc, char * argv [];
{
.....
}
In the form of function parameters, it contains an integer and a pointer array. After a C source program is compiled and linked, the extension is generated. EXE executable file, which can be directly run in the operating system. In other words, it is started by the system. Since the main () function cannot be called or transmitted by other functions
The system passes the parameters when it starts running.
In the operating system environment, a complete running command consists of two parts: the command and the corresponding parameters. The format is:
Command Parameter 1 parameter 2... parameter n accept ¿
This format is also called command line. The command in the command line is the name of the executable file, which is separated by a space and further supplemented by the command, that is, the parameter passed to the main () function.
The command line has the following relationship with the parameters of the main () function:
Set Command Behavior: program str1 str2 str3 str4 str5
In this example, programis the file name, and a Executable File program.exe generated after being linked by program.c. Each of the following parameters is followed by five parameters. For the main () function, its parameter argc records the number of commands and parameters in the command line, a total of six. The size of the pointer array is determined by the value of the parameter argc, it is char * argv [6]. The value of the pointer array is 6-15.
Each pointer to an array points to a string. It should be noted that each pointer to the received pointer array is received from the beginning of the command line. The first received is the command, followed
Is the parameter.
The following example shows how to use the main () function with parameters.
[Example 6-29] use the graph library function to draw a variable ring. It divides a circle with a radius of R 1 into n portions.
Then draw n circles with the radius of R s as the center of each equi-cent point (detailed theory of drawing this textbook chapter 9th
This document is for simple analysis ). Li ...... remaining full text>

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.