Programmer self-cultivation: the cornerstone of program running-C/C ++ Runtime Library

Source: Internet
Author: User

A c Language Runtime Library provides the following functions:

1. Start and exit: including the entry function and other functions on which the entry function depends

2. Standard Functions: these functions are implemented by the C language standard library.

3. I/O: encapsulation and implementation of Io Functions

4. Heap: encapsulation and implementation of heap

5. Language implementation: implementation of some special functions in the language.

6. debugging: code for debugging.

 

Among the components of these runtime libraries, the C language standard occupies a major role.

1. Variable Length Parameter

The function is implemented as follows:

# DEFINE _ crt_va_start (AP, V) (AP = (va_list) _ addressof (v) + _ intsizeof (v )) <br/> // va_start fetch the address of the first parameter and move the pointer up on the stack </P> <p> # DEFINE _ crt_va_arg (AP, T) (* (T *) (AP + = _ intsizeof (t)-_ intsizeof (t) <br/> // va_arg moves the pointer first, take the previous value. <br/> // Note: T indicates the type to be passed in. The distance of the pointer to be moved should be determined. </P> <p> # DEFINE _ crt_va_end (AP) (AP = (va_list) 0) <br/> // set the AP pointer to 0 </P> <p> // The stack principle is used. <br/>

 

# Include <stdio. h> <br/> # include <stdarg. h> <br/> int sum (int ,...) <br/>{< br/> int V = 0; <br/> va_list AP; <br/> va_start (AP, ); <br/> while (A --) <br/> V + = va_arg (AP, INT); <br/> va_end (AP); <br/> return V; <br/>}</P> <p> int main () <br/> {<br/> printf ("% d", sum (9, 1, 2, 3, 4, 5, 6, 7, 8, 9); <br/> return 0; <br/>}

 

2. Non-local jump

# Include <setjmp. h> <br/> jmp_buf B; </P> <p> void F () <br/>{< br/> longjmp (B, 1 ); <br/>}</P> <p> int main () <br/> {<br/> If (setjmp (B )) <br/> printf ("world! "); <Br/> else <br/> {<br/> printf (" hello "); <br/> F (); <br/>}< br/> return 0; <br/>}

Result: Hello world!

When longjmp is called, it runs to the location returned by setjmp and sets the return value of setjmp to 1.

 

Questions about CRT of different versions

1. Why can't a program compiled with vc2008 run on another machine?

Because the programs compiled by vc2008 use the manifest mechanism, these programs must depend on the corresponding version of The Runtime Library.

One solution is to use static links without relying on the crt dll.

Another method is to publish the corresponding version of The Runtime Library and program to the end user.

 

2. If different OBJ files or DLL files in a program use different CRT, will there be a problem?

If only OBJ does not have a DLL, It is a static link. Different OBJ uses different versions of static CRT during compilation. This is generally not a problem because the target file references the static library.Only keep a mark in the symbol table of the target file, No link,No static library version information.

However, if DLL files of different versions are included, problems may occur.

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.