double* to void*? Doesn't seem to work?

Started by
9 comments, last by bakery2k1 17 years, 6 months ago
Trying to cast from a double to a void*. My code works for int, but I can't get the double cast to fully work. Any ideas would be awesome.

//void** d is a parm to this function

double* dblData = (double*)malloc(sizeof(double));
void* varData=NULL;

*dblData = getDblValue(); //set dblData

varData = malloc(sizeof(double));

*((double*)varData) = *dblData;
*d = varData;


] So if you sub double with int, this works. So any idea what i'm doing wrong? Cheers [Edited by - _Sigma on October 5, 2006 9:17:21 PM]
Advertisement
Quote:Original post by _Sigma
Trying to cast from a double to a void*. My code works for int, but I can't get the double cast to fully work.


Define "doesn't fully work". Do you get a compiler error? Is it a run-time problem?
My appologies. It doesn't crash or what not, but it spews out a funny number when I recast the void* to double*. 6.7 turns into -32748126 (roughly).
I've tried to incorporate your code into a small test program.
*d = varData;
This line will not compile for me - you are dereferencing a "void *" pointer.
I made a small mistake, d is a void**. Does this change anything? =)Sorry!
So, how are you using this? This prints 6.7 correctly for me:

#include <stdio.h>double getDblValue(){	return 6.7;}void TestFunc(void ** d){	double* dblData = (double*)malloc(sizeof(double));	void* varData=NULL;	*dblData = getDblValue(); //set dblData	varData = malloc(sizeof(double));	*((double*)varData) = *dblData;	*d = varData;}int main(){	void * pd;	TestFunc(&pd);	printf("%f\n", *(double *)pd);	return 0;}


EDIT: Probably the more important question is "what are you actually trying to acheive here?". This code seems very convoluted.
Ooh nice! Thanks a ton! Seems to work fine now. I'd r++ you, but with your 1337 rating, i think that would be more of an insult!

Cheers m8
Quote:Original post by _Sigma
My appologies. It doesn't crash or what not, but it spews out a funny number when I recast the void* to double*. 6.7 turns into -32748126 (roughly).
maybe its not a bug in the above code, but a bug in the way you're printing out the value?
I've found myself stuck on a bug for ages wondering WhyTF is this value garbage!!, oh wait, im using %d instead of %f...
Allways question authority......unless you're on GameDev.net, then it will hurt your rating very badly so just shut the fuck up.
Quote:Original post by PhilMorton
Quote:Original post by _Sigma
My appologies. It doesn't crash or what not, but it spews out a funny number when I recast the void* to double*. 6.7 turns into -32748126 (roughly).
maybe its not a bug in the above code, but a bug in the way you're printing out the value?
I've found myself stuck on a bug for ages wondering WhyTF is this value garbage!!, oh wait, im using %d instead of %f...

Well it was that too :P But there was indeed a bug in the above code. =)
Quote:Original post by _Sigma
Ooh nice! Thanks a ton! Seems to work fine now. I'd r++ you, but with your 1337 rating, i think that would be more of an insult!

Cheers m8


OK, so the maximum rating you can have on these forums is 1337? Interesting... I was hoping to get above 2K someday, but I guess that would't be 1337. Is there some kind of 00ber1337 rating?
deathkrushPS3/Xbox360 Graphics Programmer, Mass Media.Completed Projects: Stuntman Ignition (PS3), Saints Row 2 (PS3), Darksiders(PS3, 360)

This topic is closed to new replies.

Advertisement