b******n 发帖数: 592 | 1 has anyone experienced pointer overflow in C++. I have a
void * ptr
what I am doing in C++ is :
ptr = reinterpret_cast(ptr)[size]
ugly, but I don't know how to do it prettier.
and the ptr overflowed.
0x7f7ea69f443d is base address
477856 is size
the result is:
0xa69f4440
I found it is very diffult to use raw pointer in C++. For example, if I want
to align the address, I can't do ptr &= ~0xff, something like this.
any suggestion? |
b******n 发帖数: 592 | 2 Sorry, I mis-read traces from my program.
My question is: how to do bit operation on pointers in C++?
I am just trying to adjust pointers by:
ptr &= ~0xf; /* C */
can't seem to do it in C++ |
t****t 发帖数: 6806 | 3 你是想干嘛啊
想把void* ptr转换成char*? 好象也不是这个意思啊?
【在 b******n 的大作中提到】 : has anyone experienced pointer overflow in C++. I have a : void * ptr : what I am doing in C++ is : : ptr = reinterpret_cast(ptr)[size] : ugly, but I don't know how to do it prettier. : and the ptr overflowed. : 0x7f7ea69f443d is base address : 477856 is size : the result is: : 0xa69f4440
|
b******n 发帖数: 592 | 4 我有个void *ptr,我现要做
ptr = (ptr + 0xf) & ~0xf
C下面很简单的,一到C++就不行了,value overflow了
【在 t****t 的大作中提到】 : 你是想干嘛啊 : 想把void* ptr转换成char*? 好象也不是这个意思啊?
|
t****t 发帖数: 6806 | 5 因为很明显你的指针是64位的
你可以这么写
ptr=reinterpret_cast((reinterpret_cast(ptr) + 0xful )
& ~ 0xful))
【在 b******n 的大作中提到】 : 我有个void *ptr,我现要做 : ptr = (ptr + 0xf) & ~0xf : C下面很简单的,一到C++就不行了,value overflow了
|
P********e 发帖数: 2610 | 6 64位是不是LONG默认就是64bit的,那longlong呢
)
【在 t****t 的大作中提到】 : 因为很明显你的指针是64位的 : 你可以这么写 : ptr=reinterpret_cast((reinterpret_cast(ptr) + 0xful ) : & ~ 0xful))
|
t****t 发帖数: 6806 | 7 there is no LONG or long long in current C++ standard.
in 64-bit compilers, *usually* long is 64 bit.
【在 P********e 的大作中提到】 : 64位是不是LONG默认就是64bit的,那longlong呢 : : )
|
b******n 发帖数: 592 | 8 i didn't try 0xful, i tried
reinterpret_cast((reinterpret_cast(ptr)+0xf) & ~f))
give me very strange result. I don't know why this behave differently. Now I
implemented without bit operation and it works. I will try your method late
r and let you know if it works
)
【在 t****t 的大作中提到】 : 因为很明显你的指针是64位的 : 你可以这么写 : ptr=reinterpret_cast((reinterpret_cast(ptr) + 0xful ) : & ~ 0xful))
|
t****t 发帖数: 6806 | 9 of course yours won't work.
~0xf (not ~f) is type int (usually 0xfffffff0), then this int is converted
to unsigned long, where sign extension is NOT performed, which gives you (
0x00000000fffffff0), not what you wanted.
In other words, if you used reinterpret_cast, it will work. int->long
will preform sign extension, which converts ~0xf correctly to (
0xfffffffffffffff0).
The type of constant in C is sometimes incorrectly ignored.
I
late
【在 b******n 的大作中提到】 : i didn't try 0xful, i tried : reinterpret_cast((reinterpret_cast(ptr)+0xf) & ~f)) : give me very strange result. I don't know why this behave differently. Now I : implemented without bit operation and it works. I will try your method late : r and let you know if it works : : )
|
b******n 发帖数: 592 | 10 That's why. thanks. I thought 0xf will be long as well.
long
【在 t****t 的大作中提到】 : of course yours won't work. : ~0xf (not ~f) is type int (usually 0xfffffff0), then this int is converted : to unsigned long, where sign extension is NOT performed, which gives you ( : 0x00000000fffffff0), not what you wanted. : In other words, if you used reinterpret_cast, it will work. int->long : will preform sign extension, which converts ~0xf correctly to ( : 0xfffffffffffffff0). : The type of constant in C is sometimes incorrectly ignored. : : I
|