请帮助我理解指针值的按位操作

发布于 2024-11-03 08:53:17 字数 241 浏览 1 评论 0原文

我无法理解为什么 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 技术交流群。

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

发布评论

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

评论(5

月下客 2024-11-10 08:53:17
sizeof (long)     = (0)00000100
sizeof(long)-1    = (0)00000011
~(sizeof(long)-1) = (1)11111100

因此,2 位设置为 0 使地址与 4 字节对齐。另外,它主要在地址已经增加了 sizeof(long)-1 时使用

sizeof (long)     = (0)00000100
sizeof(long)-1    = (0)00000011
~(sizeof(long)-1) = (1)11111100

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

哭泣的笑容 2024-11-10 08:53:17

它被强制转换为 long,因为 (1) 除了强制转换之外,您不能对 void* 执行任何操作;(2) 在作者的平台上,void * 值恰好适合 long。他确实应该使用 uintptr_tsize_t 来代替。

这段代码的作用:

sizeof(long) - 1

很可能是 3 或 7,具体取决于平台。

~(sizeof(long) - 1)

是一个位掩码,选择除最后几位之外的所有位。

((long)addr & ~(sizeof(long) - 1))

addr 向下舍入/对齐以寻址 long 大小的块。发生舍入的原因是最后 lg(3) 或 lg(7) 位为零,而其余位是从 addr 复制的(其中 lg 是整数二进制对数)。

It's cast to long because (1) you can't do any operations on a void* except cast it and (2) on the author's platform, a void* value just so happens to fit in a long. He should really have used uintptr_t or size_t instead.

What the piece of code does:

sizeof(long) - 1

is most likely either 3 or 7, depending on the platform.

~(sizeof(long) - 1)

is a bitmask that selects all but the last few bits.

((long)addr & ~(sizeof(long) - 1))

is addr rounded down/aligned to address a long-sized chunk. Rounding occurs because the last lg(3) or lg(7) bits are zeros while the rest is copied from addr (where lg is integer binary logarithm).

口干舌燥 2024-11-10 08:53:17

这是一种非常丑陋且不可移植的做法,

peakAddr = (char *)addr - ((uintptr_t)addr & -(uintptr_t)sizeof(long));

请注意,原始版本不仅依赖于指向 long 和返回的指针的成功往返转换,而且还依赖于 size_tsizeof 运算符的结果类型)与 long 宽度相同或更宽。如果不是,用 ~ 生成的位掩码将在高位进行零扩展并删除指针的一部分。

基本上,您应该在心里记下,无论您在其中发现什么程序都是糟糕的代码,并且不要将其视为想法的来源......

This is a really ugly, unportable way of doing

peakAddr = (char *)addr - ((uintptr_t)addr & -(uintptr_t)sizeof(long));

Note that the original version not only relies on successful round trip conversion of pointers to long and back, but also on size_t (the type of the result of the sizeof operator) being the same width or wider than long. 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...

緦唸λ蓇 2024-11-10 08:53:17

你基本上使 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

海夕 2024-11-10 08:53:17

这在某些编译器上会出现错误,其中 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 a long. 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.

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