Jump to content
  • Advertisement
Sign in to follow this  
OpenGL_Guru

Warning: 64-bit machines and using malloc

This topic is 4823 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 will save some poor old soul in here who uses malloc to allocate memory who will inevitably start to program on 64-bit machines. maybe you already do at work and you use the new keyword instead of malloc but after a day or so of messing with this problem i will save somoene the headache. i was porting a 32-bit app written in C(not written by me) to a 64-bit machine. everything should work fine right? WRONG! heres a snippit of code on a 64-bit machine: #include <stdio.h> #include <math.h> float *dat; int x = 100; int y = 200; {.... ..... //...do some stuff dat = (float *) malloc (sizeof(float) * x * y); printf("%f\n", dat[0]); ..... .... return 1; } so you would think that you would get an echo of whatever was in dat[0] right? i mean even if it's junk you should get something. no, actually you dont, you get a segmentation fault, ok that is half the answer.. the other answer is why? before i give the answer maybe someone can take a guess as to what is wrong. i am not a C programmer so ive always used the new keyword so ive never ran into this problem. but i will tell you, you learn A LOT of stuff just by porting programs to and from 32/64 bit machines. ok have at it :)

Share this post


Link to post
Share on other sites
Advertisement
The memory allocation looks fine to me but the code is attempting to print a float as an integer (%d).
Try using the %f or %g format instead, or casting the value to an integer before sending it to printf.

I'm not so sure why it actually crashed though. Probably some change in the calling convention, but I don't know enough about how the architecture treats varargs and floats to attempt any guesses.

Share this post


Link to post
Share on other sites
you still will get a seg fault, i changed it to %f though. i have already solved the problem, i was wondering if anyone else could figure it out. the 64-bit compiler will indeed give you a warning at this point..but thats all you will get...this one is a bit nasty for those that are C++ programmers. a C programmer in here might pick up on this ...:)

Share this post


Link to post
Share on other sites
float* will give a 64 bit pointer and I guess malloc returns a 64 bit pointer and printf by default thinks it is looking at a 32 bit value...

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
just a guess... malloc returns 32bit pointer and 64 bit machine needs 64 bit pointer

Share this post


Link to post
Share on other sites
Quote:
Original post by Trip99
float* will give a 64 bit pointer and I guess malloc returns a 64 bit pointer and printf by default thinks it is looking at a 32 bit value...



tripp you ALMOST got it...haha thats CLOSE..

Share this post


Link to post
Share on other sites
The only thing that jumps out at me is it could be an alignment issue. Most of the 64-bit architectures are more pedantic when it comes to alignment than IA32, but it would be a pretty dumb malloc implementation which didn't align it's return value on a 8-byte boundary...

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!