Cast pointer to int

Hello!

My question is pretty is pretty much like in the topic. I have a typedef like this:
 
typedef ptr * some_t;


I want to assign int to it (and vice versa):
1
2
3
some_t x;
int y = 3;
y = x;


The only solution I came up with that shuts compiler up is:
 
y  = *reinterpret_cast<some_t *>(&x);


But it looks kind of ugly. Can someone recommend me some workaround? Thanks! :)
Why do you need to convert between integers and pointers?
Hi :) I need to assign ID to assign an Id to the pointer :)
There has to be a better way for this.
Wrap it in a class, would that work?

So, you have an object and you can call the function .id() which returns the adress converted to an int.

furthermore, you realise that you assign an uninitialised pointer to y?
1
2
3
some_t x;
int y = 3;
y = x;
Last edited on
Hello!

But even if I'll use a function, I still have to do the conversion in that function somehow and the code I wrote is very very ugly :/

Yes I realize that, it was just an example :) some_t x is actually function parameter :)
Found solution :) The proper way is to cast to uintptr_t. This of course required me to change int to uint32_t/uint64_t but since it's only Id, doesn't really matter :)

1
2
some_t x;
uint32_t y = reinterpret_cast<uintptr_t>(x);
But even if I'll use a function, I still have to do the conversion in that function somehow and the code I wrote is very very ugly :/
Yeah but that's the whole point of writing classes and functions anyway...
Take a look in std::vector, it's not very fancy but the API is beautiful.

Found solution :) The proper way is to cast to uintptr_t. This of course required me to change int to uint32_t/uint64_t but since it's only Id, doesn't really matter :)
If that's good enough for you...

i'd write a function that returns the id of everything
1
2
3
4
5
template <typename T>
std::size_t id(const T& data)
{
    return reinterpret_cast<std::size_t>(&data);
}


Then you can write your code like this:

1
2
int i = 5;
int i_id = id(i);


which is much better readable than yours
Last edited on
Hi! :)

First, a small remark: I don't want to use address of something as id, I want to use value of pointer as id.

Second, regarding readability: the code I have is already in a function, check my comment above:
some_t x is actually function parameter :)

My question was about the cast, not about how to put the code in a function, so I think your quest to fix my code was not really necessary.

And finally, your code will only work if size_t is equal or greater than the size of pointer, which (I can assure you) is not always true. On the contrary, uintptr_t is guaranteed to have size big enough to store a pointer.
First, a small remark: I don't want to use address of something as id, I want to use value of pointer as id.
I think I don't understand, that's the same in my opinion...
1
2
3
4
int i = 5;
int* p = &i;
std::cout << p << std::endl;
std::cout << &i << std::endl;


And finally, your code will only work if size_t is equal or greater than the size of pointer, which (I can assure you) is not always true.
size_t is allways big enough to hold any pointer value in your system.
standard library reference wrote:
It is a type able to represent the size of any object in bytes: size_t is the type returned by the sizeof operator and is widely used in the standard library to represent sizes and counts.
The size of an object may be as big as your RAM and the biggest possible pointer value is the size of your RAM - 1 therefore size_t is allways large enough.

My question was about the cast, not about how to put the code in a function, so I think your quest to fix my code was not really necessary.
Sorry if I offended you by trying to give you tips on how to make your code more readable.

Second, regarding readability: the code I have is already in a function, check my comment above:
some_t x is actually function parameter :)
Well, wrapping it in a function makes the code in the function more readable and less ugly.
If you just cast it and someone reads it he'll think "What's happening?"
If you wrap it in a function and just call id(val) it will be clear what you are doing.

Last edited on
The size of an object may be as big as your RAM and the biggest possible pointer value is the size of your RAM - 1 therefore size_t is allways large enough.
Not actually always the case in systems using segmented memory. Actual ponter might be larger than size of largest object put in memory (16bit size_t vs 32bit pointer).

So cast to uintptr_t is a good idea. And also you can change your ID to be uintptr_t too: it is an integer in the end.

Still I do not see the need of pointer at all. Why don't you store all in integers, as you said you do not actually need to handle addresses? Or just use pointer as ID without casting around?

Last edited on
I probably don't understand what Beju is trying to do, and I know this has already been marked as solved, but just in case this might help: If you want a unique ID for any generated thing, I would go back to using a function, you could call it get_unique_ID(), and inside it, you'd have something like
1
2
3
4
5
size_t get_unique_ID()
{
    static size_t i = 0;
    return i++;
}
Topic archived. No new replies allowed.