Warning: 64-bit machines and using malloc

Started by
59 comments, last by Anon Mike 18 years, 7 months ago
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 :)
heh
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.
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 ...:)
heh
What are the values of x and y?
any number.. say 100 for x and 200 for y
heh
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...
------------------------See my games programming site at: www.toymaker.info
just a guess... malloc returns 32bit pointer and 64 bit machine needs 64 bit pointer
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..
heh
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...
Would this work:

printf("%fI64\n",dat[0]); ?

no scrub that - hmmmmm
------------------------See my games programming site at: www.toymaker.info

This topic is closed to new replies.

Advertisement