Jump to content
  • Advertisement
Sign in to follow this  
rogerdv

problem casting void* to GLint in 64bits system

This topic is 3625 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

Im trying to use Cal3d in my project and I have found a weird problem. Cal3d API returns some values as void*, that should be casted to GLint like this: glBindTexture(GL_TEXTURE_2D, (GLint)pCalRenderer->getMapUserData(0)); This works on some systems but in others the compiler produces an error: cast from void* to GLint loses precision. The only common thing I can find between the 2 setups where I get the error is that they are both 64 bits systems (Ubuntu and Gentoo). Can someboy suggest me a solution for this, or at least the cause of the problem?

Share this post


Link to post
Share on other sites
Advertisement
Casting between a pointer type and an integer type is very bad programming practice. Especially since it can completely break on 64bit systems - as you discovered.

GLint is 32bit. A pointer (such as void *) is 32bit on a 32bit system, and 64bit on a 64bit system. So on the latter, you are trying to force a 64bit value (the pointer) into a 32bit integer. Doesn't work.

Share this post


Link to post
Share on other sites
Is this actually an error? Or a warning? As Yann said; it's bad either way, but if it's a warning you can at least ignore it or disable the error. If the library is casting from int to pointer and returning the pointer, then the cast back to int should be safe (I think, I'm not sure if it works with negative numbers)

Share this post


Link to post
Share on other sites
Seems that Cal3d handles generating texture and for some reason they store the value as UserData (a void*). I solved the compiler error casting to long instead of GLint. Dont know if it has some side effect.

Share this post


Link to post
Share on other sites
It's badness, but it shouldn't fail for any reason I can see. GLuint is 32 bit, void* is either 32 or 64 bit, so worst case is casting from 64 to 32, where no data can be lost because the original value was 32 bit.

Share this post


Link to post
Share on other sites
The original code isnt mine. I took it from Cal3d demo because it is much better optimized as it uses arrays instead of basic primitives. It worked fine until I found this problem. In cal3d mailing list I didnt got any answer about it from developers.
It is use Cal3d or find somebody to write mesh file loading/animation code.

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.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!