C++如何获取void指针中存储的地址?

发布于 2024-08-07 20:06:16 字数 175 浏览 9 评论 0原文

如何获取指针指向的值的内存地址?就我而言,它是一个空指针。 只是将它分配给一个 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 技术交流群。

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

发布评论

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

评论(5

没有你我更好 2024-08-14 20:06:16
std::size_t address = reinterpret_cast<std::size_t>(voidptr);
// sizeof(size_t) must be greater or equal to sizeof(void*)
// for the above line to work correctly.

@谢保罗
我认为在这个特定问题中将 void* 转换为 size_t 就足够了,原因有以下三个:

  • 提问者没有具体说明他是否
    是否需要便携式解决方案。他
    说这对他有用。我不知道具体是什么
    这意味着但是
    我很清楚他是
    在 Windows 或其他操作系统上使用 IA-32
    系统处于保护模式。那
    意味着将指针转换为
    整数是定义的运算
    那个系统,即使它不是由标准 C++ 定义的。

  • 其次,我建议首先将指针转换为 int ,正如 litbjalf 向我展示的那样,这显然毫无意义。我纠正了我所犯的错误,并将 int 替换为 size_t

  • 最后,我努力寻找与您在标准中提出的解决方案相关的内容。不幸的是,我找不到任何相关的东西。我有这个参考:ANSI ISO IEC 14882 2003。我认为 sellibitze 指出它将成为即将推出的标准的一部分。我对C确实不太了解,显然C99引入了这个完美的解决方案。我希望有人向我展示 C++ 中的可移植解决方案。

请随时纠正我的错误,我仍然是大学的学生:)

谢谢,

std::size_t address = reinterpret_cast<std::size_t>(voidptr);
// sizeof(size_t) must be greater or equal to sizeof(void*)
// for the above line to work correctly.

@Paul Hsieh
I think it is sufficient to convert void* to size_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 replaced int with size_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,

星軌x 2024-08-14 20:06:16

C 和 C++ 中的指针某种抽象形式的内存地址的体现。您可以获得指针的特定于实现的字符串解释,如下所示:

printf ("%p", voidPointer);

另一种方法是将其转换为 intptr_t:

(intptr_t) voidPointer;

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:

printf ("%p", voidPointer);

Another way is to cast it to intptr_t:

(intptr_t) voidPointer;

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.

轮廓§ 2024-08-14 20:06:16

您需要一个强制转换 - 要么是reinterpret_cast(首选):

UInt32 address = reinterpret_cast<UInt32>( voidPointer );

要么是 C 风格强制转换(确实不推荐):

UInt32 address = (UInt32)voidPointer;

You need a cast - either a reinterpret_cast (preferable):

UInt32 address = reinterpret_cast<UInt32>( voidPointer );

or a C-style cast (really not recommended):

UInt32 address = (UInt32)voidPointer;
一萌ing 2024-08-14 20:06:16

您可以使用 sprintf() 获取地址的字符串表示形式,或使用 %p 格式说明符通过 printf() 显示它,例如:

printf( "%p", voidptr ) ;

如果您只想对地址执行指针算术,请将其转换为适当的具体数据类型指针,否则按照已经建议的方式将其转换为整数。

You can get a string representation of the address with sprintf() or display it with printf() using the %p format specifier, e.g:

printf( "%p", voidptr ) ;

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.

荭秂 2024-08-14 20:06:16

在 C++ 中,我可以使用以下命令打印指向的地址:

cout << "Address: " << ((unsigned)my_ptr);

In C++ I was able to print the pointed address with:

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