指针算术(Pointer arithmetics)

编程入门 行业动态 更新时间:2024-10-28 13:21:57
指针算术(Pointer arithmetics)

不确定标题是否正确,但我相信问题就在那里。 我有这段代码:

FILE_DIRECTORY_INFORMATION *Buffer; Buffer = ExAllocatePoolWithTag (NonPagedPool, 4096, 'arK'); ..<fill in data into the buffer>.. //Values here are: Buffer:0x81490000; NextEntryOffset:0x48 Buffer += Buffer->NextEntryOffset; //Values here are: Buffer:0x81491440; NextEntryOffset:0x0

问题是,最后一个代码行执行乘法而不是简单添加。 新的缓冲区值应该是(或者至少我希望它是:) 81490048但是81491440(81490000 + 48 * 48)。 谁能解释我为什么?

PS:我使用Windbg检查了所有值。 使用VS11编译,最后一行代码行真正翻译成imul指令。

not sure if the title is correct but I believe problem is there. I have this piece of code:

FILE_DIRECTORY_INFORMATION *Buffer; Buffer = ExAllocatePoolWithTag (NonPagedPool, 4096, 'arK'); ..<fill in data into the buffer>.. //Values here are: Buffer:0x81490000; NextEntryOffset:0x48 Buffer += Buffer->NextEntryOffset; //Values here are: Buffer:0x81491440; NextEntryOffset:0x0

Problem is that instead of simple adding, the last code line performs multiplying. The new Buffer value should be (or at least I would wish it to be:) 81490048 but is 81491440 (81490000+48*48). Can anyone explain me why?

PS: I checked all the values using Windbg. Compiled using VS11, last code line is really translated into imul instruction.

最满意答案

这就是指针算法在C中的工作原理。将“X”添加到指针实际上意味着将指针X的大小添加到起始内存位置。

我想你可能会把一个临时的char *扔回去,但我想不出一个原因。

That's how pointer arithmetic works in C. Adding 'X' to a pointer actually means adding the size of the pointee X times to the starting memory location.

I think you can probably cast to a temporary char * and back, but I can't think of a reason.

更多推荐

本文发布于:2023-04-27 21:45:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1328628.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:算术   指针   arithmetics   Pointer

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!