这两种访问数组的方法是否实现相同?

发布于 2024-10-02 23:06:49 字数 194 浏览 0 评论 0原文

我们有一个如下所示的三字节数组:

char charArray[3];
charArray[1]='a';
//or
char * charP=charArray;
charP[1]='a';

使用两个方法来访问数组的第二个元素,现在编译器会以相同的方式实现它们还是第一个方法不会像第二个方法那样涉及指针?

We have a three-byte array like below:

char charArray[3];
charArray[1]='a';
//or
char * charP=charArray;
charP[1]='a';

Two methods are used to access to second element of array, now will they be implemented in the same way by compiler or the first method will not involve a pointer like second method?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

离鸿 2024-10-09 23:06:49

编译器会以同样的方式实现它们

如果使用数组的名称并且下标是常量表达式,则编译器可能可以在编译时执行部分或全部指针运算。使用指向数组的指针可能无法执行相同的操作;这可能取决于编译器是否可以确定指针指向数组中的元素。

但这只是猜测。确定给定编译器是否为两者发出相同代码的唯一方法是查看该编译器发出的代码。

will they be implemented in the same way by compiler

Maybe.

If you use the name of an array and the subscript is a constant expression, a compiler can probably do some or all of the pointer arithmetic at compile-time. It might not be able to do the same using a pointer to an array; that probably depends on whether or not the compiler can tell for sure that the pointer points to an element in the array.

This is just speculation, though. The only way to tell for sure whether a given compiler emits the same code for both is to look at the code emitted by that compiler.

如若梦似彩虹 2024-10-09 23:06:49

第二种方法可能会使编译器生成更多代码。 charP 是一个指针,需要一个变量,尽管使用少量代码可能会进行编译器优化。

对于 charP,它是一个左值,您可以将其移动到指向另一个位置,并且可以使用 ++ 等对其进行递增。

The second method may make the compiler generate more code. charP is a pointer and needs a variable for it, although with that small amount of code there may be a compiler optimisation.

with charP, it is an l-value and you can move it to point to another location and you can increment it with ++, etc.

浮萍、无处依 2024-10-09 23:06:49

从语义上讲,它们的行为相同 - 将设置第二个元素 - 但没有指定编译器如何实现这一点。

编译器可能会选择将第一个数组访问实现为带有偏移量的指针引用,并对第二个数组执行相同的操作。另一方面,它可能会优化本地分配的数组访问并直接引用适当的内存位置 - 同样,它可能会对第二次访问进行相同的优化,但也可能不会。它具有的选项取决于正在编译的二进制平台,以及它是否“意识到”数组或指针实际上指向局部变量(您希望它指向局部变量)。

由于无论如何您都无法区分差异,因此您通常可以认为它们是相同的,即使实现恰好不同。

Semantically, they'll behave the same - the second element will be set - but it's not specified as to how the compiler will achieve that.

It's possible the compiler could choose to implement the first array access as a pointer reference with an offset, and do the same for the second. On the other hand, it might optimize the locally allocated array access and directly reference the appropriate memory location - and again, it may do the same optimization on the second access, but it might not. The options it has depend on the binary platform being compiled for, and whether it "realizes" that the array or pointer actually point to a local variable (which you'd expect it to).

Since you cannot tell the difference anyhow, you can generally consider them to be identical, even if the implementation happens to differ.

如歌彻婉言 2024-10-09 23:06:49

看看吧!

检查编译器使用不同优化级别生成的汇编代码。我保证这样你会学到很多东西。

Look and see!

Examine the assembly code that your compiler generates with different optimization levels. I guarantee you'll learn a lot that way.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文