为什么C/C++语言使用指针?

时间:2014-10-17 11:12来源:cricode 作者:瓏曦 点击:

这个问题一问出来,直接被面试官秒杀了,面试官大神,你怎么不按套路出牌啊?

说好的malloc和new的区别呢?说好的const和#define有什么优缺点呢?说好的进程和线程有什么区别和联系呢?说好的进程间通信有哪些方式呢?说好的%¥%#……@……*&()#!@#*……“……#%#%#呢?

说好的这些面试题,统统都没有。一上来就来这么一个问题。加上本身语言表达能力不够好,当时的心情就是这样的:不淡定中带点伤感!

说好的奥斯卡影帝呢?

 

现在想想,这个问题就是一个大坑。

首先,这个题目理解起来就有点猫腻。言外之意好像是想让你说C/C++中有指针,而C#或者Java等语言中没有指针。将这些编程语言做一下对比。

假装沉思了3秒钟,然后我就想当然的,顺着这么个思路,就开始顺口开河了。C#是高级语言,没有指针啥啥的就开始了。反反复复那么几句话,怎么扯也扯不出个清晰的逻辑出来。说出来的答案连自己都觉得是bullshit。

现在回头想想,当时的理解和答案是大错特错了。这本身就就是一个错误的问题。或者说,面试官就是故意将你往沟里带,等着你中套。

答案是:每一种编程语言都使用指针。不止C/C++使用指针。

为什么这样说?

因为后来在网上搜索答案时,在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.

每一种编程语言都使用指针。C++只是将指针暴露给了用户(程序员),而Java和C#等语言择是将指针给隐藏起来了。

但糟糕的是,有些语言试着将指针隐藏起来,却露出了尾巴,有时候让人非常费解。

下面是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

WARNING:面试时要时刻保持头脑的清醒,淡定,千万不要被面试官给引到沟里去了!

分享到:

凌阳教育嵌入式培训——全国唯一“原厂嵌入式培训”机构,全国唯一“按班公布学员就业去向”的诚信机构

关注我们:

全国免费咨询电话:156-0117-5697或010-62981113(转2824) 服务监督电话:010-62981113-2800

京ICP备09010168号  京公网安备11010802010586号

北京嵌入式培训中心:海淀区上地信息产业基地中黎科技园1号楼3层A段

Copyright © 2017 SunplusEdu Inc. All Rights Reserved