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

What are the 12 interesting questions and answers in C language?

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article shows you 12 interesting questions and answers in C language, which are concise and easy to understand, which will definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.

0gimgets () method

Q: there is a hidden problem in the following code. Can you find it?

A: the obscure problem is the use of the gets () method. This method accepts an string type parameter, but does not detect whether this value has enough space to copy the data. So here we generally use the fgets () method in the future.

# include int main (void) {char buff [10]; memset (buff,0,sizeof (buff)); gets (buff); printf ("\ n The buffer entered is [% s]\ n", buff); return 0;}

1Based strcpy () method

Q: password protection is a very basic function, see if you can handle the following code?

# include int main (int argc, char * argv []) {int flag = 0; char passwd [10]; memset (passwd,0,sizeof (passwd)); strcpy (passwd, argv [1]); if (0 = = strcmp ("LinuxGeek", passwd)) {flag = 1;} if (flag) {printf ("\ n Password cracked\ n") } else {printf ("\ n Incorrect passwd\ n");} return 0;}

2the return type of the main () method

Q: can the following code be compiled? If so, is there any problem hidden in this code?

# include void main (void) {char* ptr = (char*) malloc (10); if (NULL = = ptr) {printf ("\ n Malloc failed\ n"); return;} else {/ / Do some processing free (ptr);} return;}

A: the answer is that the code can be compiled, but it leaves a warning about the return type of the main () method. The real return type of the main () method should be 'int' instead of' void'. This is because the 'int' return type allows the program to return a status value. This status value will be more important especially when this program is used as an adjunct to other applications.

3. Memory leak

Q: is there a memory leak in the following code?

# include void main (void) {char* ptr = (char*) malloc (10); if (NULL = = ptr) {printf ("\ n Malloc failed\ n"); return;} else {/ / Do some processing} return;}

A: well, although the above code does not free the pointer ptr, it actually does not cause a memory leak even when the program ends, because when the program ends, all the memory occupied at the beginning is cleared. But if the above code is in the while loop, it will cause serious problems.

Note: if you need to know more about memory leaks and how to use tools to detect memory leaks, you can refer to this article Valgrind

4Bing free () method

Q: why does the following code fail when the user enters' freeze', but works fine if the user enters' zebra'?

# include int main (int argc, char* argv []) {char* ptr = (char*) malloc (10); if (NULL = = ptr) {printf ("\ n Malloc failed\ n"); return-1;} else if (argc = = 1) {printf ("\ nUsage\ n") } else {memset (ptr, 0,10); strncpy (ptr, argv [1], 9); while (* ptr! ='z') {if (* ptr = =') break; else ptr++ } if (* ptr = ='z') {printf ("\ n String contains 'z'\ n"); / / Do some more processing} free (ptr);} return 0;}

A: the root of the problem is that the code changes the address of the ptr pointer in the while loop. When typed as' zebra', the while loop ends even before the * * loop is executed, so the memory address freed by free () is the address assigned by malloc () in the first place. But when you type 'freeze', the address recorded by ptr is changed in the while loop because the wrong address is passed to the free () method causing a crash.

5,atexit with _ exit

Q: in the following code, the atexit () method is not called. Do you know why?

# include void func (void) {printf ("\ n Cleanup function called\ n"); return;} int main (void) {int I = 0; atexit (func); for (; I

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