解除引用指针的代价是多少? [英] How expensive is it to dereference a pointer?

查看:114
本文介绍了解除引用指针的代价是多少?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对指针执行取消引用操作有多昂贵?



我可以想象内存传输与对象大小成正比,但我想

解决方案

解除引用在翻译为机器代码时,可能意味着不同的东西,做与取消引用的对象。通过指针访问类的单个成员通常是便宜的。例如,如果c是指向类C 的实例的 int 成员n的指针,则类似这样: / p>

  int n = c-> n; 

可能转换为一个或两个机器指令,并且可能通过单个存储器访问加载一个寄存器。 p>

另一方面,这意味着对c:

指向的对象进行完整的复制。

  C d = * c; 

这样做的代价取决于C的大小,但请注意,



请注意,访问大对象的成员通常需要指针偏移计算以及存储器访问,无论对象是否是本地对象。通常只有非常小的对象被优化为仅存在于寄存器中。



如果你关心指针在引用上的代价,那么就不是。这些之间的区别是语言语义差异,当机器代码生成时,指针和引用访问看起来完全一样。


How expensive is it to perform the dereference operation on a pointer?

I can imagine that the memory transfer is somehow proportional to the object size, but I want to know how expensive the dereference operation part is.

解决方案

Dereferencing, when translated into machine code, can mean different things depending on what you do with the dereferenced object. Accessing a single member of a class through a pointer is typically cheap. For example if c is a pointer to an instance of class C with an int member n then something like this:

int n = c->n;

Might translate into one or two machine instructions and might load a register with a single memory access.

On the other hand this implies making a complete copy of the object pointed to by c:

C d = *c;

The cost of this will depend on the size of C, but note that it is the copy that is the major expense and the 'dereference' part is really just 'using' the pointer address in the copy instructions.

Note that accessing members of large objects typically requires pointer offset calculation and memory access whether or not the object is a local object or not. Typically only very small objects are optimized to live only in registers.

If you are concerned about the cost of pointers over references then don't be. The difference between these are a language semantics difference and by the time the machine code is generated pointer and reference access look exactly the same.

这篇关于解除引用指针的代价是多少?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆