数组语法与指针语法和代码生成? [英] Array-syntax vs pointer-syntax and code generation?

查看:82
本文介绍了数组语法与指针语法和代码生成?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在书中,Richard的 理解和使用C指针" 里斯(Reese)在第85页说,

In the book, "Understanding and Using C Pointers" by Richard Reese it says on page 85,

int vector[5] = {1, 2, 3, 4, 5};

vector[i]生成的代码与*(vector+i)生成的代码不同.符号vector[i]生成机器代码,该机器代码从位置向量,从该位置移动 i的位置开始,并使用其内容.符号*(vector+i)生成机器代码,该机器代码从位置vector开始, i添加到该地址,然后使用该地址处的内容.虽然结果相同,但是生成的机器代码却不同.这种差异对大多数程序员而言几乎没有意义.

The code generated by vector[i] is different from the code generated by *(vector+i) . The notation vector[i] generates machine code that starts at location vector , moves i positions from this location, and uses its content. The notation *(vector+i) generates machine code that starts at location vector , adds i to the address, and then uses the contents at that address. While the result is the same, the generated machine code is different. This difference is rarely of significance to most programmers.

您可以看到

You can see the excerpt here. What does this passage mean? In what context would any compiler generate different code for those two? Is there a difference between "move" from base, and "add" to base? I was unable to get this to work on GCC -- generating different machine code.

推荐答案

引号是错误的.悲剧性的是,这种垃圾仍然在这个十年中被出版.实际上,C标准将x[y]定义为*(x+y).

The quote is just wrong. Pretty tragic that such garbage is still published in this decade. In fact, the C Standard defines x[y] as *(x+y).

页面后面有关左值的部分也是完全错误的.

The part about lvalues later on the page is completely and utterly wrong too.

恕我直言,使用这本书的最好方法是将其放入回收箱或燃烧.

IMHO, the best way to use this book is to put it in a recycling bin or burn it.

这篇关于数组语法与指针语法和代码生成?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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