C++如何获取void指针中存储的地址?
如何获取指针指向的值的内存地址?就我而言,它是一个空指针。 只是将它分配给一个 uint 会给我这个错误:
Error 1 error C2440: 'return' : cannot convert from 'void *' to 'UInt32'
谢谢!
how can i get the memory address of the value a pointer points to? in my case it is a void pointer.
just assigning it to an uint gives me this error:
Error 1 error C2440: 'return' : cannot convert from 'void *' to 'UInt32'
thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
@谢保罗
我认为在这个特定问题中将
void*
转换为size_t
就足够了,原因有以下三个:提问者没有具体说明他是否
是否需要便携式解决方案。他
说这对他有用。我不知道具体是什么
这意味着但是
我很清楚他是
在 Windows 或其他操作系统上使用 IA-32
系统处于保护模式。那
意味着将指针转换为
整数是定义的运算
那个系统,即使它不是由标准 C++ 定义的。
其次,我建议首先将指针转换为
int
,正如 litb 和 jalf 向我展示的那样,这显然毫无意义。我纠正了我所犯的错误,并将int
替换为size_t
。最后,我努力寻找与您在标准中提出的解决方案相关的内容。不幸的是,我找不到任何相关的东西。我有这个参考:ANSI ISO IEC 14882 2003。我认为 sellibitze 指出它将成为即将推出的标准的一部分。我对C确实不太了解,显然C99引入了这个完美的解决方案。我希望有人向我展示 C++ 中的可移植解决方案。
请随时纠正我的错误,我仍然是大学的学生:)
谢谢,
@Paul Hsieh
I think it is sufficient to convert
void*
tosize_t
in this specific question for three reasons:The questioner didn't specify if he
wants a portable solution or not. He
said, that it worked with him. I don't know exactly what
that means but
it is clear to me he is
working on IA-32 on Windows or other
system under protected mode. That
means converting a pointer to an
integer is a defined operation on
that system even if it is not defined by standard C++.
Second, I proposed first converting the pointer to
int
which is clearly meaningless as litb and jalf showed me. I corrected the mistake I've done, and replacedint
withsize_t
.Finally, I tried my hard to find something relevant to what you proposed as a solution in the standards. Unfortunately, I couldn't find anything relevant. I have this reference: ANSI ISO IEC 14882 2003. I think sellibitze pointed out that it will be part of the coming standards. I really don't know about C, and obviously C99 introduced this perfect solution. I would like someone to show me a portable solution in C++.
Please, don't hesitate to correct my mistakes, I am still a student at uni :)
Thanks,
C 和 C++ 中的指针是某种抽象形式的内存地址的体现。您可以获得指针的特定于实现的字符串解释,如下所示:
另一种方法是将其转换为 intptr_t:
C++ 中的 NULL 指针定义为等于“0”,因此您可能会在以上操作。然而,这仍然取决于您以及您必须将其解释为内存地址的任何特定于实现的知识。
关键是,在使用奇怪的内存模型(例如 16 位 x86 DOS 编译器)的平台中,这些平台将带有偏移量的段作为其内存模型,可以通过多种方式解释指针。在原始地址表示中,NULL 可以是 DS:0 或 SS:0 而不仅仅是普通的 0。
A pointer, in C and C++ is the embodiment of a memory address in some abstract form. You can get a implementation specific string interpretation of a pointer as:
Another way is to cast it to intptr_t:
The NULL pointer in C++ is defined as equal to "0", so presumably you would get the equiavlent of (intptr_t) 0 in the above operation. However it is still up to you and whatever implementation specific knowledge you have to interpret that as a memory address.
The point is that in platforms that use bizarre memory models such as 16 bit x86 DOS compilers, that have segments with offsets as their memory model, pointers can be interpreted in many ways. In a raw address representation NULL could be DS:0 or SS:0 rather than just a plain 0.
您需要一个强制转换 - 要么是
reinterpret_cast
(首选):要么是 C 风格强制转换(确实不推荐):
You need a cast - either a
reinterpret_cast
(preferable):or a C-style cast (really not recommended):
您可以使用 sprintf() 获取地址的字符串表示形式,或使用 %p 格式说明符通过 printf() 显示它,例如:
如果您只想对地址执行指针算术,请将其转换为适当的具体数据类型指针,否则按照已经建议的方式将其转换为整数。
You can get a string representation of the address with sprintf() or display it with printf() using the %p format specifier, e.g:
If you just want the address to perform pointer arithmetic on, cast it to an appropriate concrete data type pointer, otherwise cast it to an integer as already suggested.
在 C++ 中,我可以使用以下命令打印指向的地址:
In C++ I was able to print the pointed address with: