由一道面試題所引出的C語(yǔ)言static變量特性
最近部門在準(zhǔn)備春招筆試題時(shí),有這樣一道題目:用C/C++語(yǔ)言實(shí)現(xiàn)一個(gè)函數(shù),給定一個(gè)int類型的整數(shù),函數(shù)輸出逆序的整數(shù)對(duì)應(yīng)的字符串,例如輸入1234,則輸出字符串"4321",,輸入-1234,則輸出字符串"-4321"。題目要求,不使用標(biāo)準(zhǔn)庫(kù),以及不能分配動(dòng)態(tài)內(nèi)存。當(dāng)時(shí)覺得蠻簡(jiǎn)單的,這不就是類似字符串逆轉(zhuǎn)嘛,紙上得來終覺淺,絕知此事要躬行,自己嘗試做了一下,發(fā)現(xiàn)還是有一些地方值得注意。今天在此整理一下常見的坑,鞏固下基礎(chǔ)東西。
版本一
算法思路其實(shí)很簡(jiǎn)單:使用對(duì)10取余和除法操作依次獲取每一位的數(shù)字,然后根據(jù)ASSIC碼轉(zhuǎn)換為字符,將結(jié)果存放在一個(gè)char型數(shù)組中,***返回字符串?dāng)?shù)組結(jié)果,如下所示:
- #include<stdio.h>
- //版本一
- const char * reverseInt(int n)
- {
- char str[16] = {0};
- int temp = n;
- int i = 0;
- if (n < 0)
- {
- temp = -n;
- str[i++] = '-';
- }
- //當(dāng)temp除到是一位數(shù)的時(shí)候退出
- while (0 != temp / 10)
- {
- char ch = temp % 10 + 48;
- temp = temp / 10;
- str[i++] = ch;
- }
- //處理原始數(shù)據(jù)的***位
- str[i++] = temp % 10 + 48;
- return str;
- }
- int main(int argc, char **agrv)
- {
- int test_data1 = 12345;
- int test_data2 = 789;
- printf("[test_data1] %d--->%s\n",
- test_data1, reverseInt(test_data1));
- printf("[test_data2] %d--->%s\n",
- test_data2, reverseInt(test_data2));
- return 0;
- }
發(fā)現(xiàn)編譯出現(xiàn)了警告,如下:
- [root@epc.baidu.com ctest]# gcc -g -o test test.c
- test.c: In function 'reverseInt':
- test.c:24:2: warning: function returns address of local variable [-Wreturn-local-addr]
- return str; ^
從編譯器給出的信息很清楚的說明了問題:返回了一個(gè)局部變量的地址,但是我們知道,函數(shù)的局部變量是存在stack中的,當(dāng)這個(gè)函數(shù)調(diào)用過程結(jié)束時(shí),這個(gè)局部變量都是要釋放掉的,自然就不可再使用了,所以就會(huì)產(chǎn)生這樣的warning,這個(gè)是和變量的生命周期相關(guān)的。
版本二
對(duì)于版本一存在的問題,很自然的會(huì)想到有兩種解決方案,***:使用malloc分配動(dòng)態(tài)內(nèi)存存放結(jié)果,但是題目中明確說明不能不能分配動(dòng)態(tài)內(nèi)存。因此自然排除掉。第二種方案就是將char result[16]改為static型:static char result[16];對(duì),就是這么一點(diǎn)改動(dòng)。
- #include<stdio.h>
- //版本二
- const char * reverseInt(int n)
- {
- static char str[16] = {0};
- int temp = n;
- int i = 0;
- if (n < 0)
- {
- temp = -n;
- str[i++] = '-';
- }
- //當(dāng)temp除到是一位數(shù)的時(shí)候退出
- while (0 != temp / 10)
- {
- char ch = temp % 10 + 48;
- temp = temp / 10;
- str[i++] = ch;
- }
- //處理原始數(shù)據(jù)的***位
- str[i++] = temp % 10 + 48;
- return str;
- }
- int main(int argc, char **agrv)
- {
- int test_data1 = 12345;
- int test_data2 = 789;
- printf("[test_data1] %d--->%s\n",
- test_data1, reverseInt(test_data1));
- printf("[test_data2] %d--->%s\n",
- test_data2, reverseInt(test_data2));
- return 0;
- }
運(yùn)行結(jié)果如下:
- [root@epc.baidu.com ctest]# ./test
- [test_data1] 12345--->54321
- [test_data2] 789--->98721
從運(yùn)行結(jié)果上看,***個(gè)測(cè)試數(shù)據(jù)其結(jié)果是正確的,但是第二個(gè)輸出結(jié)果確實(shí)錯(cuò)誤的。這是什么原因?先來回一下用static修飾所修飾的局部變量(也稱靜態(tài)局部變量)特點(diǎn),如下:
1:靜態(tài)局部變量定義時(shí)未賦初值,則默認(rèn)初始化為0;
2:靜態(tài)局部變量其作用域?yàn)楹瘮?shù)或代碼塊,其生命周期為整個(gè)程序的運(yùn)行期間;注意這兩個(gè)概念不要混淆;
3:在一個(gè)進(jìn)程的運(yùn)行期間,靜態(tài)局部變量只會(huì)初始化一次,就是***次調(diào)用該靜態(tài)局部變量所在函數(shù)的時(shí)候初始化,此后再調(diào)用不會(huì)初始化。
好了,到這里,其實(shí)問題的原因已經(jīng)很明顯了:在上面程序中,static char str[16] = {0}只會(huì)初始化一次,既在執(zhí)行reverseInt(test_data1)時(shí)初始化,執(zhí)行完該語(yǔ)句,將結(jié)果存放到str中,此時(shí)str中的內(nèi)容為54321,既str[16] = {'5','4','3','2','1','\0'};當(dāng)再次對(duì)第二個(gè)測(cè)試數(shù)進(jìn)行轉(zhuǎn)換調(diào)用reverseInt(test_data2)時(shí),str仍然是上次的結(jié)果{'5','4','3','2','1','\0'},因此在轉(zhuǎn)換后為98721。
版本三
那么如何解決版本二的問題了,一個(gè)很簡(jiǎn)單的辦法就是在reverseInt函數(shù)中對(duì)static變量str每次使用for循環(huán)進(jìn)行初始化,如下,鑒于篇幅,就不將main函數(shù)也貼出來了:
- const char * reverseInt(int n)
- {
- static char str[16] = {0};
- int temp = n;
- int i = 0;
- int j = 0;
- for (; j < 16; j++)
- {
- str[j] = '\0';
- }
- if (n < 0)
- {
- temp = -n;
- str[i++] = '-';
- }
- //當(dāng)temp除到是一位數(shù)的時(shí)候退出
- while (0 != temp / 10)
- {
- char ch = temp % 10 + 48;
- temp = temp / 10;
- str[i++] = ch;
- }
- //處理原始數(shù)據(jù)的***位
- str[i++] = temp % 10 + 48;
- return str;
- }
運(yùn)行,能得到我們期望的結(jié)果了:
- [root@epc.baidu.com ctest]# ./test
- [test_data1] 12345--->54321
- [test_data2] 789--->987
其實(shí),版本三還有很多細(xì)節(jié)需要考慮的,比如:當(dāng)輸入的整數(shù)超過int的范圍如何處理等等,雖然是小細(xì)節(jié),但卻十分重要,大家有興趣可以思考下練練手。