由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - pointer overflow
相关主题
弱问内存的问题C++ Q93 - Q95 (转载)
a question about bitwise operationint F::*x = &F::x是什么意思?
unsigned int subtract intc++ 里用到pointer 的地方我们尽可能用smart pointer吗?
[合集] can one type cast reference in C++不如各位高手挑个专题讲讲C++11吧
C++class指针转换Go adopts JavaScript’s idea of semicolon insertion
Why do I need to use "plain" pointer?问题: C++ static_cast between int and float
is smart_ptr really that good?[合集] reinterpret_cast a 4 byte unsigned char to integer
C++ Q05: pointer to constant variable[合集] 编译报错stack overflow,到底来自何处
相关话题的讨论汇总
话题: ptr话题: c++话题: 0xf话题: long
进入Programming版参与讨论
1 (共1页)
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

1 (共1页)
进入Programming版参与讨论
相关主题
[合集] 编译报错stack overflow,到底来自何处C++class指针转换
借人气问个c++的overflow (转载)Why do I need to use "plain" pointer?
经典题atoi的溢出处理 (转载)is smart_ptr really that good?
一个integer promotion问题C++ Q05: pointer to constant variable
弱问内存的问题C++ Q93 - Q95 (转载)
a question about bitwise operationint F::*x = &F::x是什么意思?
unsigned int subtract intc++ 里用到pointer 的地方我们尽可能用smart pointer吗?
[合集] can one type cast reference in C++不如各位高手挑个专题讲讲C++11吧
相关话题的讨论汇总
话题: ptr话题: c++话题: 0xf话题: long