Linux: entry function and program Initialization

Source: Internet
Author: User
Article Title: linux: entry function and program initialization. Linux is a technology channel of the IT lab in China. Includes basic categories such as desktop applications, Linux system management, kernel research, embedded systems, and open source.

If we compare a program to a world, the startup of the program is undoubtedly "creation ". In this chapter, we will begin with the creation of a program and access to another type of group that silently serves the program. They can enable the program to start normally and make various functions that we are familiar with play a role. They are the runtime libraries of the application.

 11.1 entry functions and program Initialization

11.1.1 does the program start from main?

Just as Christians believe that the world was born after seven days of creation, any qualified C/C ++ programmer should know the fact that the program starts from the main function. But is the truth True? If you are good at observation, you will find that when the program executes the first line of the main function, many things have been completed:

 

From the code, we can see that the initialization process of the global variable has ended when the program was just executed to main (the value of a has been determined ), the two parameters (argc and argv) of the main function are also correctly passed in. In addition, when you don't know, the heap and stack Initialization is completed quietly, and some system I/O is also initialized, so you can safely use printf and malloc.

  
Here, the constructor of object v and the function foo used to initialize the global variable g are called before main.

[Tie Zheng 3] atexit is also a special function. Atexit accepts a function pointer as a parameter and ensures that the function pointed to by this function is called when the program Exits normally (that is, the exit function is returned from the main or called. For example:

Void foo (void)

{

Printf ("bye! \ N ");

}

Int main ()

{

Atexit (& foo );

Printf ("endof main \ n ");

}

The call time of the function registered with the atexit function is after the end of main, so the output of this Code is:

Endof main

Bye!

All these facts provide unfavorable evidence for "main creation": after the operating system loads the program, the first line of code to run is not the first line of main, but some other code, these codes prepare the environment required for executing the main function and call the main function. In this case, you can write various types of code in the main function with confidence: apply for memory, use system calls, trigger exceptions, and access I/O. After the main function is returned, it records the return value of the main function, calls the function registered by atexit, and ends the process.

 

[1] [2] [3] [4] [5] [6] [7] [8] [9] [10]... Next page>

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.