请帮助我理解指针值的按位操作
我无法理解为什么 addr 被类型转换为 long,然后用表达式补充。基本上整行都涉及 peekAddr 的计算
void *addr;
char *peekAddr ;
peekAddr = (char *) ((long)addr & ~(sizeof(long) - 1 ) ) ;
peekWord = ptrace( PTRACE_PEEKDATA, pid, peekAddr, NULL ) ;
I am not able to understand why is addr being typecasted to long, and then complemented with expression.. basically the whole line involving the calculation of peekAddr
void *addr;
char *peekAddr ;
peekAddr = (char *) ((long)addr & ~(sizeof(long) - 1 ) ) ;
peekWord = ptrace( PTRACE_PEEKDATA, pid, peekAddr, NULL ) ;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
因此,2 位设置为 0 使地址与 4 字节对齐。另外,它主要在地址已经增加了 sizeof(long)-1 时使用
so 2 bits set to 0 make the address aligned to 4 bytes. additionally it's mostly used when the address was already incremented by sizeof(long)-1
它被强制转换为
long
,因为 (1) 除了强制转换之外,您不能对void*
执行任何操作;(2) 在作者的平台上,void *
值恰好适合long
。他确实应该使用uintptr_t
或size_t
来代替。这段代码的作用:
很可能是 3 或 7,具体取决于平台。
是一个位掩码,选择除最后几位之外的所有位。
addr
向下舍入/对齐以寻址long
大小的块。发生舍入的原因是最后 lg(3) 或 lg(7) 位为零,而其余位是从addr
复制的(其中 lg 是整数二进制对数)。It's cast to
long
because (1) you can't do any operations on avoid*
except cast it and (2) on the author's platform, avoid*
value just so happens to fit in along
. He should really have useduintptr_t
orsize_t
instead.What the piece of code does:
is most likely either 3 or 7, depending on the platform.
is a bitmask that selects all but the last few bits.
is
addr
rounded down/aligned to address along
-sized chunk. Rounding occurs because the last lg(3) or lg(7) bits are zeros while the rest is copied fromaddr
(where lg is integer binary logarithm).这是一种非常丑陋且不可移植的做法,
请注意,原始版本不仅依赖于指向
long
和返回的指针的成功往返转换,而且还依赖于size_t
(sizeof
运算符的结果类型)与long
宽度相同或更宽。如果不是,用~
生成的位掩码将在高位进行零扩展并删除指针的一部分。基本上,您应该在心里记下,无论您在其中发现什么程序都是糟糕的代码,并且不要将其视为想法的来源......
This is a really ugly, unportable way of doing
Note that the original version not only relies on successful round trip conversion of pointers to
long
and back, but also onsize_t
(the type of the result of thesizeof
operator) being the same width or wider thanlong
. If it's not, the bitmask generated with~
would zero-extend in the upper bits and obliterate part of the pointer.Basically, you should make a mental note that whatever program you found this in is bad code and not look to it as a source of ideas...
你基本上使 peekAddr 始终与 sizeof(long) 地址对齐。该行生成一个位掩码和二进制并将其发送到查看地址。
该行从 peekAddr 中剥离最后的 sizeof(long)-1 位。
马里奥
you basically make peekAddr always aligned on sizeof(long) adresses. the line generates a bitmask and binary ands this to the peek address.
The line strips the last sizeof(long)-1 bits from the peekAddr.
hth
Mario
这在某些编译器上会出现错误,其中
sizeof(long) < sizeof(char*)
,例如 Microsoft 的。sizeof(long)-1
正在创建一个与long
的大小相对应的位掩码。这是一个仅适用于 2 的幂的数字的技巧。前面的~
将其反转,因此现在它是所有地址位的掩码,当您尝试更改时,这些地址位应保持不变对齐地址。按位&
正在清除地址的底部位以使其对齐。This will have a bug on some compilers where
sizeof(long) < sizeof(char*)
, such as Microsoft's.sizeof(long)-1
is creating a bit mask corresponding to the size of along
. This is a trick that only works on numbers that are a power of 2. The~
in front inverts it, so now it's a mask of all the address bits that should remain unchanged when you're trying to align an address. The bitwise&
is clearing the bottom bits of the address to make it align.