12個有趣的c語言問答

0,gets() 方法

Q:以下代碼有個被隱藏住的問題,你能找到它嗎?

A:這個不顯眼的問題就是使用了 gets() 方法。此方法接受一個string類型參數,但是卻沒有檢測此數值是否 有足夠的空間來拷貝數據。所以這裏我們一般用 fgets() 方法將來的更好。

1
2
3
4
5
6
7
8
9
10
11
12
13
#include<stdio.h>
 
int main(void)
{
char buff[10];
memset(buff,0,sizeof(buff));
 
gets(buff);
 
printf("\n The buffer entered is [%s]\n",buff);
 
return 0;
}

1,strcpy() 方法

Q:密碼防護是很基本的功能,看看能否搞定下面這段代碼?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include<stdio.h>
 
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;
}
#include<stdio.h>
 
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;
}

2,main() 方法的返回類型

Q:請問下面這段代碼能否通過編譯?如果能的話,那麼這段代碼中隱含什麼問題嗎?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include<stdio.h>
 
void main(void)
{
char *ptr = (char*)malloc(10);
 
if(NULL == ptr)
{
printf("\n Malloc failed \n");
return;
}
else
{
// Do some processing
 
free(ptr);
}
 
return;
}
#include<stdio.h>
 
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:答案是代碼能通過編譯,但是會留下針對main()方法的返回類型的警告。main()方法的真正返回類型應該爲’int’而非’void’。這是因爲’int’返回類型能夠讓程序返回狀態值。尤其是當這段程序作爲其他應用的附屬程序時這個狀態值將更加重要。

3,內存泄露

Q:請問以下代碼有內存泄露嗎?

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

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include<stdio.h>
 
void main(void)
{
char *ptr = (char*)malloc(10);
 
if(NULL == ptr)
{
printf("\n Malloc failed \n");
return;
}
else
{
// Do some processing
}
 
return;
}

A:好,雖然上面的代碼沒有對指針 ptr 進行內存釋放,但實際上即使是程序結束也不會造成內存泄露,因爲當程序結束時所有一開始被佔據的內存就全部清空了。但如果上面這段代碼是在 while 循環裏面那將會造成嚴重的問題

Note: 如果你需要了解更多關於內存泄露的問題以及如何使用工具檢測內存泄露,你可以參考這篇文章 Valgrind

4,free() 方法

Q:以下代碼當用戶輸入’freeze’時會奔潰,而如果輸入’zebra’則運行正常,這是爲什麼?

2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include<stdio.h>
 
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("\n Usage \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;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include<stdio.h>
 
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("\n Usage \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:問題的根源是因爲代碼在while循環中改變了 ptr 指針的地址。當輸入爲’zebra’時,while循環甚至在執行 第一遍前就結束了,所以free()釋放的內存地址就是一開始malloc()分配的地址。但是當輸入’freeze’時, ptr記錄的地址在while循環中被更改,因爲將會是錯誤的地址傳遞到free()方法中引起崩潰。

5,atexit with _exit

Q:在以下代碼,atexit()方法並沒有被調用,你知道爲什麼嗎?

2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include<stdio.h>
 
void func(void)
{
printf("\n Cleanup function called \n");
return;
}
 
int main(void)
{
int i = 0;
 
atexit(func);
 
for(;i<0xffffff;i++);
 
_exit(0);
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include<stdio.h>
 
void func(void)
{
printf("\n Cleanup function called \n");
return;
}
 
int main(void)
{
int i = 0;
 
atexit(func);
 
for(;i<0xffffff;i++);
 
_exit(0);
}

A:這是因爲使用了 _exit() 方法。此方法並沒有調用清除數據相關的方法,比如 atexit()等。

6,void* 與 C 結構體

Q:能否設計一個方法接受任意類型的參數然後返回整數?同時是否有辦法傳遞多個這樣的參數?

A:一個能接受任意類型參數的方法像下面這個樣子:

int func(void *ptr)

 

1
int func(void *ptr)

如果需要傳遞多個參數,那麼我們可以傳遞一個包含這些參數的結構體

7,* 與 ++ 操作符

Q:以下代碼將輸出什麼?爲什麼?

#include<stdio.h>
 
int main(void)
{
char *ptr = "Linux";
printf("\n [%c] \n",*ptr++);
printf("\n [%c] \n",*ptr);
 
return 0;
}

 

1
2
3
4
5
6
7
8
9
10
#include<stdio.h>
 
int main(void)
{
char *ptr = "Linux";
printf("\n [%c] \n",*ptr++);
printf("\n [%c] \n",*ptr);
 
return 0;
}

A:以上的輸出將是:

因爲++與 * 的優先級一樣,所以 *ptr++ 將會從右向左操作。按照這個邏輯,ptr++ 會先執行然後執行*ptr。所以第一個結果是’L'。也因爲 ++ 被執行了,所以下一個printf() 結果是’i'。

8,Making changes in Code segment

Q:以下代碼運行時一定會崩潰,你能說出原因嗎?

#include<stdio.h>
 
int main(void)
{
char *ptr = "Linux";
*ptr = 'T';
 
printf("\n [%s] \n", ptr);
 
return 0;
}

 

1
2
3
4
5
6
7
8
9
10
11
#include<stdio.h>
 
int main(void)
{
char *ptr = "Linux";
*ptr = 'T';
 
printf("\n [%s] \n", ptr);
 
return 0;
}

A:這是因爲,通過 *ptr = ‘T’,此行代碼嘗試更改只讀內存存儲的字符串’Linux’。此操作當然行不通所以纔會造成崩潰。

9,Process that changes its own name

Q:你能否寫一個程序在它運行時修改它的名稱?

  1. #include<stdio.h> 

  2.  

  3. int main(int argc, char *argv[]) 

  4.     int i = 0; 

  5.     char buff[100]; 

  6.  

  7.     memset(buff,0,sizeof(buff)); 

  8.  

  9.     strncpy(buff, argv[0], sizeof(buff)); 

  10.     memset(argv[0],0,strlen(buff)); 

  11.  

  12.     strncpy(argv[0], "NewName", 7); 

  13.  

  14.     // Simulate a wait. Check the process 

  15.     // name at this point. 

  16.     for(;i<0xffffffff;i++); 

  17.  

  18.     return 0; 

 

A:以下代碼可以完成

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include<stdio.h>
 
int main(int argc, char *argv[])
{
int i = 0;
char buff[100];
 
memset(buff,0,sizeof(buff));
 
strncpy(buff, argv[0], sizeof(buff));
memset(argv[0],0,strlen(buff));
 
strncpy(argv[0], "NewName", 7);
 
// Simulate a wait. Check the process
// name at this point.
for(;i<0xffffffff;i++);
 
return 0;
}

A,局部變量的返回地址

Q:下面的代碼有問題嗎?如果有,如何修改?

  1. #include<stdio.h> 

  2.  

  3. int* inc(int val) 

  4.   int a = val; 

  5.   a++; 

  6.   return &a; 

  7.  

  8. int main(void

  9.     int a = 10; 

  10.     int *val = inc(a); 

  11.     printf("\n Incremented value is equal to [%d] \n", *val); 

  12.  

  13.     return 0; 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include<stdio.h>
 
int* inc(int val)
{
int a = val;
a++;
return &a;
}
 
int main(void)
{
int a = 10;
 
int *val = inc(a);
 
printf("\n Incremented value is equal to [%d] \n", *val);
 
return 0;
}

A:雖然上面的代碼有時運行會很好,但是在方法 inc() 中有很嚴重的隱患。當inc()方法執行後,再次使用局部變量的地址就會造成不可估量的結果。解決之道就是傳遞變量a的地址給main()。

B,處理 printf() 參數

Q:以下代碼輸出請問是什麼?

  1. #include<stdio.h> 

  2.  

  3. int main(void

  4.     int a = 10, b = 20, c = 30; 

  5.     printf("\n %d..%d..%d \n", a+b+c, (b = b*2), (c = c*2)); 

  6.  

  7.     return 0; 

 

1
2
3
4
5
6
7
8
9
10
#include<stdio.h>
 
int main(void)
{
int a = 10, b = 20, c = 30;
 
printf("\n %d..%d..%d \n", a+b+c, (b = b*2), (c = c*2));
 
return 0;
}

A:輸出將是 110,40,60

1
110..40..60

這是因爲參數都是從右向左處理的,然後打印出來卻是從左向右。

發佈了25 篇原創文章 · 獲贊 9 · 訪問量 2萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章