12个有趣的C语言面试题-1

上一篇 / 下一篇  2012-09-10 09:30:35 / 个人分类:C++

51Testing软件测试网DYeg r t|

  摘要:12个C语言面试题,涉及指针、进程、运算、结构体、函数、内存,看看你能做出几个!51Testing软件测试网V`}m qi

51Testing软件测试网VOkB8G RdB

  1、gets()函数51Testing软件测试网D l.fM2v!nS+F8o$@7w

51Testing软件测试网)i r7k/Ux'M

  问:请找出下面代码里的问题:51Testing软件测试网 |+n9[0yzR+C)H

  1. #include<stdio.h> 
  2. int main(void
  3.     char buff[10]; 
  4.     memset(buff,0,sizeof(buff)); 

  5. 1m-u9D(yMj t n_0
  6.     gets(buff); 
  7. 51Testing软件测试网Z!Nd!Q)Q$F\0sa+sgf+A
  8.     printf("\n The buffer entered is [%s]\n",buff); 
  9. 51Testing软件测试网$J3a+tE*Q W3p/P3UP/d
  10.     return 0; 
  11. }

*W/z y)qy#M0  答:上面代码里的问题在于函数gets()的使用,这个函数从stdin接收一个字符串而不检查它所复制的缓存的容积,这可能会导致缓存溢出。这里推荐使用标准函数fgets()代替。51Testing软件测试网&fL K)U$M/I5xC

51Testing软件测试网1b#Ug:]:G;A%L}GuAW

  2、strcpy()函数51Testing软件测试网A [ r$u#dS#d{

51Testing软件测试网$F\@0}V*vLZ#d

  问:下面是一个简单的密码保护功能,你能在不知道密码的情况下将其破解吗?51Testing软件测试网}0VI0h1Y[6cNo

  1. #include<stdio.h> 
  2. 51Testing软件测试网tx:~}(A9p+rD*^xp,G
  3. int main(int argc, char *argv[]) 
  4.     int flag = 0; 
  5.     char passwd[10]; 

  6. 2l&~IQK3x%c#l O0
  7.     memset(passwd,0,sizeof(passwd)); 
  8. 51Testing软件测试网;Y2h+]:cs#{"m;O Kz1D
  9.     strcpy(passwd, argv[1]); 

  10. U8kXQ4JN0
  11.     if(0 == strcmp("LinuxGeek", passwd)) 
  12.     { 
  13.         flag = 1; 
  14.     } 

  15. )zxT)l3o0{ X0
  16.     if(flag) 
  17.     { 
  18.         printf("\n Password cracked \n"); 
  19.     } 
  20.     else 
  21.     { 
  22.         printf("\n Incorrect passwd \n"); 
  23. 51Testing软件测试网4^@5T\.y
  24.     } 
  25.     return 0; 
  26. }

pWYE:Ig+^@0   答:破解上述加密的关键在于利用攻破strcpy()函数的漏洞。所以用户在向“passwd”缓存输入随机密码的时候并没有提前检查“passwd” 的容量是否足够。所以,如果用户输入一个足够造成缓存溢出并且重写“flag”变量默认值所存在位置的内存的长“密码”,即使这个密码无法通过验 证,flag验证位也变成了非零,也就可以获得被保护的数据了。例如:

ZL.g6oW0
  1. $ ./psswd aaaaaaaaaaaaa 
  2. 51Testing软件测试网qa*|,R6{T
  3. Password cracked

R S6D9p|*j0  虽然上面的密码并不正确,但我们仍然可以通过缓存溢出绕开密码安全保护。51Testing软件测试网v/L.S^e`;{x

G_/~h"P4jPY0  要避免这样的问题,建议使用 strncpy()函数。

;z8M0t*KW Gf-pj0

)T5_ M+\7F#R7s3BE$i0  作者注:最近的编译器会在内部检测栈溢出的可能,所以这样往栈里存储变量很难出现栈溢出。在我的gcc里默认就是这样,所以我不得不使用编译命令‘-fno-stack-protector’来实现上述方案。51Testing软件测试网3oW7o3d i#y1m

Y7s%H"tr!zs0  3、main()的返回类型

+U,i!W} d&U&YLa051Testing软件测试网n$}-T&O{J5d&q

  问:下面的代码能 编译通过吗?如果能,它有什么潜在的问题吗?51Testing软件测试网$G#RV!w[#dL5K

&[i IQ3mZ9iwGV0

h8oH|#Ti,j0
  1. #include<stdio.h> 
  2.  
  3. void main(void
  4.     char *ptr = (char*)malloc(10); 
  5.  
  6.     if(NULL == ptr) 
  7.     { 
  8.         printf("\n Malloc failed \n"); 
  9.         return
  10.     } 
  11.     else 
  12.     { 
  13.         // Do some processing 
  14.         free(ptr); 
  15.     } 
  16.  
  17.     return
  18. }
51Testing软件测试网&x$@9\-Y};? @

  答:因为main()方法的返回类型,这段代码的错误在大多数编译器里会被当作警告。main()的返回类型应该是“int”而不是“void”。因为“int”返回类型会让程序返回状态值。这点非常重要,特别当程序是作为依赖于程序成功运行的脚本的一部分运行时。51Testing软件测试网!F"]"CT,a&D:D` W,^

51Testing软件测试网\%{mV{$Y

  4、内存泄露51Testing软件测试网bvzcNR1^

[{0]z4?Fx0  问:下面的代码会导致内存泄漏吗?

NB3};P`T4H7SwRp$F,Q0

4Y&Ao]rQ051Testing软件测试网})x~0npr%hx

  1. #include<stdio.h> 
  2.  
  3. void main(void
  4.     char *ptr = (char*)malloc(10); 
  5.  
  6.     if(NULL == ptr) 
  7.     { 
  8.         printf("\n Malloc failed \n"); 
  9.         return
  10.     } 
  11.     else 
  12.     { 
  13.         // Do some processing 
  14.     } 
  15.  
  16.     return
  17. }
51Testing软件测试网/qW{x+O)Mq{;[i

  答:尽管上面的代码并没有释放分配给“ptr”的内存,但并不会在程序退出后导致内存泄漏。在程序结束后,所有这个程序分配的内存都会自动被处理掉。但如果上面的代码处于一个“while循环”中,那将会导致严重的内存泄漏问题!

RXUpb6d^ i0

q%_xb/z0EnMnp0  提示:如果你想知道更多关于内存泄漏的知识和内存泄漏检测工具,可以来看看我们在Valgrind上的文章

]N;n*I Z5k4R\051Testing软件测试网,va*np.wJ

  5、free()函数51Testing软件测试网-Ua4X;Z7ils!}?*S

51Testing软件测试网kBm2D+k7B

  问:下面的程序会在用户输入'freeze'的时候出问题,而'zebra'则不会,为什么?

-Gvhe D]0

U7A$w0W} YU r$z v051Testing软件测试网"~o%XF!eIP%r

  1. #include<stdio.h> 
  2.  
  3. int main(int argc, char *argv[]) 
  4.     char *ptr = (char*)malloc(10); 
  5.  
  6.     if(NULL == ptr) 
  7.     { 
  8.         printf("\n Malloc failed \n"); 
  9.         return -1; 
  10.     } 
  11.     else if(argc == 1) 
  12.     { 
  13.         printf("\n Usage  \n"); 
  14.     } 
  15.     else 
  16.     { 
  17.         memset(ptr, 0, 10); 
  18.  
  19.         strncpy(ptr, argv[1], 9); 
  20.  
  21.         while(*ptr != 'z'
  22.         { 
  23.             if(*ptr == ''
  24.                 break
  25.             else 
  26.                 ptr++; 
  27.         } 
  28.  
  29.         if(*ptr == 'z'
  30.         { 
  31.             printf("\n String contains 'z'\n"); 
  32.             // Do some more processing 
  33.         } 
  34.  
  35.        free(ptr); 
  36.     } 
  37.  
  38.     return 0; 
  39. }
51Testing软件测试网e3|{q.K7Qa|5b

  答:这里的问题在于,代码会(通过增加“ptr”)修改while循环里“ptr”存储的地址。当输入“zebra”时,while循环会在执 行前被终止,因此传给free()的变量就是传给malloc()的地址。但在“freeze”时,“ptr”存储的地址会在while循环里被修改,因 此导致传给free()的地址出错,也就导致了seg-fault或者崩溃。

h]Yx_:x]0

'[8m x0VY:x e ^$U$l0  6、使用_exit退出51Testing软件测试网S\l:~w_Q0@

#m ^#a/jgT S0  问:在下面的代码中,atexit()并没有被调用,为什么?51Testing软件测试网(~)H9`R J&rrU0MZ/a

TV.T-Km9^b;f0

,e5H?-a]0
  1. #include<stdio.h> 
  2.  
  3. void func(void
  4.     printf("\n Cleanup function called \n"); 
  5.     return
  6.  
  7. int main(void
  8.     int i = 0; 
  9.  
  10.     atexit(func); 
  11.  
  12.     for(;i<0xffffff;i++); 
  13.  
  14.     _exit(0); 
  15. }
51Testing软件测试网^k-Zc3Gc+AV

  这是因为_exit()函数的使用,该函数并没有调用atexit()等函数清理。如果使用atexit()就应当使用exit()或者“return”与之相配合。51Testing软件测试网7uK'X2y(uP@


TAG:

 

评分:0

我来说两句

Open Toolbar