為什么C/C++語(yǔ)言使用指針

這是參加面試時(shí)面試官問的一道開放式的題目。

問題:為什么C/C++語(yǔ)言使用指針?

這個(gè)問題一問出來(lái),直接被面試官給秒殺了,面試官大神,你怎么不按套路出牌呢?

說好的malloc和new的區(qū)別呢?說好的const和#define有什么缺點(diǎn)呢?說好的進(jìn)程和線程有什么區(qū)別和聯(lián)系呢?說好的進(jìn)程間通信有哪些方式呢?說好的%¥%#……@……*&()#!@#*……“……#%#%#呢?說好的這些面試題,統(tǒng)統(tǒng)都沒有。一上來(lái)就來(lái)這么一個(gè)問題。加上本身語(yǔ)言表達(dá)能力不夠好,當(dāng)時(shí)的心情就是這樣的:不淡定中帶點(diǎn)傷感!

現(xiàn)在想想,這個(gè)問題就是一個(gè)大坑。

首先,這個(gè)題目理解起來(lái)就有點(diǎn)貓膩。言外之意好像是想讓你說C/C++中有指針,而C#或者Java等語(yǔ)言中沒有指針。將這些編程語(yǔ)言做一下對(duì)比。

假裝沉思了3秒鐘,然后我就想當(dāng)然的,順著這么個(gè)思路,就開始順口開河了。C#是高級(jí)語(yǔ)言,沒有指針啥啥的就開始了。反反復(fù)復(fù)那么幾句話,怎么扯也扯不出個(gè)清晰的邏輯出來(lái)。說出來(lái)的答案連自己都覺得是bullshit。

現(xiàn)在回頭想想,當(dāng)時(shí)的理解和答案是大錯(cuò)特錯(cuò)了。這本身就就是一個(gè)錯(cuò)誤的問題?;蛘哒f,面試官就是故意將你往溝里帶,等著你中套。

答案是:每一種編程語(yǔ)言都使用指針。不止C/C++使用指針。

為什么這樣說?

因?yàn)楹髞?lái)在網(wǎng)上搜索答案時(shí),在Quora上找到了一些大神們的解答。

“Everything uses pointers. C++ just exposes them rather than hiding them,”

It’s easier to give someone an address to your home than to give a copy of your home to everyone.

每一種編程語(yǔ)言都使用指針。C++只是將指針暴露給了用戶(程序員),而Java和C#等語(yǔ)言擇是將指針給隱藏起來(lái)了。

但糟糕的是,有些語(yǔ)言試著將指針隱藏起來(lái),卻露出了尾巴,有時(shí)候讓人非常費(fèi)解。

下面是30年老程序員Marcus Geduld舉的栗子。引用如下:

Take, for instance, Javascript:

function foo( bar ) {

bar++;

}

var x = 5;

foo( x );

console.log( x );

Now, what is the value of x at then end of this code? 5 or 6?

Even though, in the function, the value of x gets assigned to bar and then incremented from 5 to 6, the log statement at the end will print 5. Why? because x’s value will be copied in to the function. In other words, bar won’t be pointing at the value of x, even though I wrote foo( x ). It will be pointing at a copy of that value.

Now, let’s say I wrote this:

function foo2( anArray ) {

anArray[ 0 ]++;

}

var myArray = [ 10, 20, 30 ];

foo2( myArray );

console.log( myArray );

In this case, the log will read [ 11, 20, 30 ]. So in the first case, the value was untouched. In this case, it’s been changed. Why, because foo2 didn’t get passed a copy of a value. Rather, it got passed a pointer—to the same array that myArray pointed to. So, in the first case, x and bar pointed to different values, whereas in the second case, myArray and anArray pointe to the same value, a pointer to [10, 20, 30 ].

Put more simply, this is a variable set to a value …

var a = 10;

Whereas this is a variable set to a pointer:

var a = [ 10 ];

But since nothing makes this explicit, you just have to learn some weird rules. And since many beginners don’t, they get hopelessly muddled. And they wind up accidentally changing values they didn’t intend to change and accidentallynot-changing values they did intend to change. Ugh!

Just in case this is unclear, compare this …

function foo( bar ) { bar++; console.log( bar ) };

var x = 5;

foo( x );

console.log( x );

output:

6

5

… with this :

function foo2( bar ) { bar[ 0 ]++; console.log( bar[ 0 ] ); }

var x = [ 5 ];

foo2( x );

console.log( x[ 0 ] );

output:

6

6

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容