Jump to content
  • Advertisement
Sign in to follow this  
supagu

64bit - point to int cast

This topic is 4349 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

i've just written some code: where nextInstance is a void* instance is a void* int* ptr = (int*)((char*)instance + variableInfo->offset); *ptr = (int)nextInstance; as soon as i finished these 2 lines of code i went, OMG this probably isnt 64bit safe, so what is the safe way to do this? unfortunately i have to cast it to a non void* as: something like the following: void* ptr = (void*)((char*)instance + variableInfo->offset); *ptr = nextInstance does not compile any ideas on making this safe?

Share this post


Link to post
Share on other sites
Advertisement
It might be helpful to know where you get nextInstance from (and why it's a pointer)

Basically, you should just try to keep your usage of the two types separate. pointers are for representing addresses. ints are for representing integer data values. If nextInstance is data you need, then it should be an int to begin with. If it's meant to contain an address, it should be a pointer all along.

At least that's what you should aim for. Since we don't know why nextInstance is a pointer in your case, it's hard to offer more advice than that.

Share this post


Link to post
Share on other sites
instance is a pointer to some arbitary class, while nextInstance is also a pointer to some data, that needs to be poked in to instance at a certain offset (ie poked in to a pointer variable of instance) obvisouly instance is an unknown class, but the offset to the pointer variable is known

Share this post


Link to post
Share on other sites
You are trying to solve a problem that seems to have been created by Really Bad Programming... Having to access an attribute in that way should never happen, and I don't think that there really is a safe way to do it.

If you know that (char*)instance + variableInfo->offset points to a pointer variable (which is actually the hard part), then you can safely write any pointer that is not a function pointer to that address. (It is not guaranteed that a function pointer fits into a void pointer. It will work on most systems, but it doesn't have to.)

From that point of view, you first try was close.

// the offset must be correct
int** ptr = (int**)((char*)instance + variableInfo->offset);
// now *ptr resolves to an int* variable
*ptr = (int*)nextInstance;

Share this post


Link to post
Share on other sites
Quote:
Original post by supagu
i've just written some code:

where nextInstance is a void*
instance is a void*

any ideas on making this safe?


The first thing I'd suggest is to forget you ever heard that there was such a thing as a void*.

Share this post


Link to post
Share on other sites
Quote:
Original post by Zahlman
Quote:
Original post by supagu
i've just written some code:

where nextInstance is a void*
instance is a void*

any ideas on making this safe?


The first thing I'd suggest is to forget you ever heard that there was such a thing as a void*.

What's a "void*"?

@supagu: store pointers in pointers. Their size may vary, and may be different from the size of an integer. The future C++0x standard might define an intptr_t type that would ease such manipulation, and you can do the same (either as a typedef or as a class - in order to ensure type safety).

Regards,

Share this post


Link to post
Share on other sites
Quote:
Original post by Shadowdancer
It is not guaranteed that a function pointer fits into a void pointer. It will work on most systems, but it doesn't have to


huh? What possible reason could there be for that?

Share this post


Link to post
Share on other sites
Quote:
Original post by DaBookshah
Quote:
Original post by Shadowdancer
It is not guaranteed that a function pointer fits into a void pointer. It will work on most systems, but it doesn't have to


huh? What possible reason could there be for that?


All pointers have the same size - including function pointers. You can safely promote a void* to a function pointer (the opposite is true too). If your compiler doesn't allow you to do this, report this as a bug to the compiler vendor. See sections 4.3 [conv.func] and 4.10§2 [conv.ptr] of the C++ standard.

I think you wanted to speak about member function pointers. They are still pointers, but they carry more information, making it impossible to convert them to a void* (or to any pointer type). However, this has nothing to do with the pointer size.

Regards,

Share this post


Link to post
Share on other sites
Quote:
Original post by supagu
any ideas on making this safe?


Any code that converts pointers everywhichway, including through char* and void*, cannot be safe. You might be able to make it less-unsafe by adding comments about what your intent is, what preconditions and postconditions are expected, and generally why no other safer, non-pointer-crazy method could not achieve the same thing.

As far as I am concerned, I would stick with:


// Allow changing the implementation of a "Location" class
// instead of using a naked reference.
template<typename Data> class Location {
Data & data;
public:
Location(Data& data) : data(data) {}
void Insert(Data& inserted) { this->data = inserted; }
};

// An example class, used to insert data at a given location.
class Pokeable {
int* memberVariable;
public:
Location<int*> GetPokeLocation() {
return Location(this->memberVariable);
}
};

// Do the poking.
void DoPoke(int* nextInstance, Pokeable& instance) {
instance.GetPokeLocation().Insert(nextInstance);
}

// Do the poking on an arbitrary class:
template<typename Data, typename Class)
void DoArbitraryPoke(Data nextInstance, Class& instance) {
instance.GetPokeLocation().Insert(nextInstance);
}

// Better poking on arbitrary class: forget about the
// class and pass the Location around.
void DoArbitraryPoke2(int* nextInstance, Location<int*> where) {
where.Insert(nextInstance);
}




The design is still ugly as hell, but at least it's safe.

Share this post


Link to post
Share on other sites
Quote:
Original post by supagu
i've just written some code:

where nextInstance is a void*
instance is a void*

int* ptr = (int*)((char*)instance + variableInfo->offset);
*ptr = (int)nextInstance;

...


Just my 2cent ...

On an 64-bit Alpha int is 32 bit and Pointers are 64 bit ...
so the seconde line of code does not look save in the situation.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!