문제

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!

도움이 되었습니까?

해결책

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,

다른 팁

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.

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;

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.

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

cout << "Address: " << ((unsigned)my_ptr);
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top