指针数学与数组索引 [英] Pointer math vs. Array index

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

问题描述

我知道这已经散列过一些时间,但我今天跟我握了握指针运算/数组索引的了解所遇到的情况。

I know this has been hashed over a number of time, but I have come across a case today that shook my understanding of the pointer math/ array index.

正如我永诺理解,和放大器; mybuff [10]和(安培; mybuff + 10)referancing相同的内存equivilent方式。

As I have allways understood it, &mybuff[10] and (&mybuff+10) are equivilent ways of referancing the same memory.

不过,我花了一上午的战斗,其中一个案例:

However I spent the morning fighting a case where:

memcpy(&mybuff+10,&in,8); 

溢出缓冲区,当与优化的编译工作只是罚款编译调试时。

overflowed the buffer, when compiled with optimization on and worked just fine when compiled for debugging.

虽然athe同时

memcpy(&mybuff[10],&in,8); 

在这两种情况下工作得很好。

worked just fine in both cases.

有任何意见或指针非常感谢。

Many thanks for any ideas or pointers.

推荐答案

http://cplusplus.com/doc/tutorial对于运营商的优先顺序/运营/

&放大器; mybuff + 10 (安培; mybuff)+10

&放大器; mybuff [10] 及(mybuff [10])

see http://cplusplus.com/doc/tutorial/operators/ for priority order of operators
&mybuff+10 like (&mybuff)+10
&mybuff[10] like &(mybuff[10])

修改

还有一些指针<一href=\"http://cplusplus.com/doc/tutorial/pointers/\">http://cplusplus.com/doc/tutorial/pointers/

这篇关于指针数学与数组索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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