Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example Analysis of Library functions in C language

2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly shows you the "sample analysis of library functions in C language", which is easy to understand and clear. I hope it can help you solve your doubts. Let me lead you to study and learn the article "sample analysis of library functions in C language".

1 getchar function that returns an integer

Code:

# includeint main () {char c; while ((c = getchar ())! = EOF) / / the return value of the getchar function is integer putchar (c); return 0;}

There are three possibilities for the above code:

Some legal input characters are truncated so that the value of c is the same as that of EOF, and the program will stop in the middle of copying.

C is simply impossible to get the value of EOF and fall into an endless loop.

The program appears to work properly, but it is entirely coincidental. Although the return result of the function geutchar "truncates" when assigned to the variable c of type char, in many compilers, instead of comparing c with EOF, they compare the return value of the getchar function with EOF! If the compiler takes this approach, the above example will work properly

2 update sequential files

When using r + to write and read files at the same time, you need to use fseek to move the pointer, because the location of the file pointer has changed while writing and reading.

3 buffer output and memory allocation

There are two ways of program output: one is instant processing, and the other is temporary storage and then large block writing.

Setbuf (stdout,buf)

Statement notifies the input / output library. All output written to stdout should use buf as the output buffer until the buf buffer is filled or the programmer calls fflush directly (for a file opened by a write operation, calling fflush will cause the contents of the output buffer to be actually written to the file) before the contents of the buf buffer are actually written to the stdout. The size of the buffer is defined by BUFSIZ in the system header file.

Here is an example:

# includeint main () {int c; char buf [BUFSIZ]; setbuf (stdout,buf); while ((c = getchar ())! = EOF) putchar (c);}

The above program is wrong because the last time the buf buffer was cleared was part of the cleanup that the C runtime library had to do after the end of the main () function before handing over control to the operating system. However, before that, the buf character array has been released.

Two solutions:

Static char buf [BUFSIZ]; setbuf (stdout, (char*) malloc (BUFSIZ)); / / there is no need to check whether the malloc function call is successful, because the second parameter of the setbuf function can be NULL, and the standard output does not need to be buffered. 4 library function

The C language implementation includes the signal library function as a way to capture asynchronous time.

# header file signal (signal type, handler function) to be referenced by include//

The signal type here represents some of the constants defined in the system header file signal.h, which are used to identify the type of signal that the signal function will capture. Here handler function is the event handler that will be called when the specified event occurs.

Note: signals may even appear during the execution of some complex library functions such as malloc. Therefore, from a security point of view, the signal processing function should not call the above type of library function.

For example, suppose the execution of the malloc function is interrupted by a signal. At this point, only part of the data structure used by malloc to keep up with the available memory is likely to be updated. If the signal processing function calls the malloc function, the result may be a complete collapse of the data structure used by the malloc function, with unimaginable consequences.

Conclusion: the signal is very complex and tricky, and it has some characteristics that are not portable in nature. So we should keep the functions handled by signal as simple as possible and organize them together so that we can easily modify them when we need to adapt to a new system.

Practice

Q: when a program terminates abnormally, the last few lines of program output are often lost. Why? What measures can we take to solve this problem?

A: an abnormally terminated program may not have a chance to empty its output buffer, so the output generated by the program may be somewhere in memory but will never be written out. On some systems, these unwritable output data can be as long as several pages.

For programmers debugging such programs, this loss of output is often misleading because it creates the impression that the program fails much earlier than it actually fails. * * the solution is to force output buffering not to be allowed during debugging. * * Solutions are as follows:

Setbuf (stdout, (char*) 0)

This statement must be executed before any output is written to stdout (including any calls to printf functions). The most appropriate place for this statement is as the first statement of the main function.

The purpose of the following program is to copy its input to output:

# includeint main () {register int c; while ((c = getchar ())! = EOF) putchar (c); return 0;}

Change the code to the following code, the program still runs correctly, but much slower, why?

# define EOF-1int main () {register int c; while ((c = getchar ())! = EOF) putchar (); return 0;}

Function calls take a long time to execute, so getchar is often implemented as a macro. This is defined in stdio.h, so a program does not include a stdio.h header file, replacing getchar macros with getchar function calls wherever fgetchar macros appear. This program slows down because of the increased overhead caused by function calls. The same basis applies to putchar.

The above is all the contents of the article "sample Analysis of Library functions in C language". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report