博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
为什么C/C++语言使用指针
阅读量:4166 次
发布时间:2019-05-26

本文共 2676 字,大约阅读时间需要 8 分钟。

这是参加面试时,面试官问的一道开放性题目。

问题是:为什么C/C++语言使用指针?

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

说好的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年老程序员举的栗子。引用如下:

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:面试时要时刻保持头脑的清醒,淡定,千万不要被面试官给引到沟里去了!

转载:

你可能感兴趣的文章
看你的linux编译系统是32位还是64位最简单的方法以及不同数据类型占用字节个数
查看>>
在Linux系统下,Vi命令模式和编辑模式如何切换?
查看>>
数据库sqlite查询语句与升序或降序
查看>>
sql判断字段是否为空
查看>>
利用typedef定义函数指针
查看>>
函数指针与链表结合举例
查看>>
数据结构基础——指针及动态内存分配(malloc)
查看>>
Linux下*.tar.gz文件解压缩命令(以及后台执行压缩命令,包括zip格式文件)
查看>>
回调函数的总结
查看>>
C++ 字节对齐的总结(原因和作用)
查看>>
Source Insight(vs2012,ultraedit) 中Tab键设置为4个空格代替
查看>>
stm32和51的区别
查看>>
Linux进程之alarm()信号传送闹钟函数
查看>>
字节转换为字符串-linux下的itoa函数和window下的spritf函数
查看>>
C++实现链表基本操作
查看>>
malloc与free
查看>>
调用malloc时发生了什么
查看>>
自己动手写内存分配函数malloc
查看>>
linux C之alarm函数
查看>>
如何自实现一个malloc函数(指定区间分配)
查看>>