d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Need Some Pointer/typecasting Help
Add Reply New Topic New Poll
Member
Posts: 6,562
Joined: Oct 29 2007
Gold: 4.00
Jan 23 2013 08:21pm
float myFunction(int *a)
{
float *b= (float *)(*a);
return *b;
}

as is, the function compiles. but when I try and plug in values and print b, the program hangs. am I doing something fundamentally wrong here?

This post was edited by Aimed_Shot on Jan 23 2013 08:22pm
Member
Posts: 690
Joined: Aug 6 2012
Gold: 275.00
Jan 24 2013 01:22am
Hello, what are you trying to do with this code?

You've declared variable b of pointer type, then you've tryied to cast int value to
pointer to a float (see no sense here) then you tryied to dereference your pointer,
but since last operation had no sence you trying to read memory at non-accessable
and got access violation.
Member
Posts: 690
Joined: Aug 6 2012
Gold: 275.00
Jan 24 2013 01:23am
if you want to convert integer to float then why use pointer?
Member
Posts: 690
Joined: Aug 6 2012
Gold: 275.00
Jan 24 2013 01:47am
Everything is fine with your code, but i think it just do not what you've expected.

Your function takes some address (as an integer) and returns float value at this address,
this code demonstrate that your code works perfectly:

float myFunction(int *a)
{
float *b = (float *)(*a);
return *b;
}

int main(int argc, char** argv)
{
float test = 3.14;
int adr = (int) &test;
float ret = myFunction(&adr);

return 0;
}

but in that case I'd better pass argument by value:

float myFunction(int a)
{
float *b = (float *) a;
return *b;
}

int main(int argc, char** argv)
{
float test = 3.14;
int adr = (int) &test;
float ret = myFunction(adr);

return 0;
}
Member
Posts: 9,803
Joined: Jun 28 2005
Gold: 6.67
Jan 24 2013 04:14pm
Quote (lbn954 @ 24 Jan 2013 08:47)
Everything is fine with your code,
It's not fine, reading an arbitrary address is undefined behaviour,

Member
Posts: 690
Joined: Aug 6 2012
Gold: 275.00
Jan 24 2013 04:22pm
Sure, I just mean that that code may be correct in some context (as in my example).

This post was edited by lbn954 on Jan 24 2013 04:22pm
Member
Posts: 9,803
Joined: Jun 28 2005
Gold: 6.67
Jan 24 2013 04:46pm
Quote (lbn954 @ 24 Jan 2013 23:22)
Sure, I just mean that that code may be correct in some context (as in my example).
Not really. It may be untrue for microcontrollers, where int might be 16-bit, and it almost definitely won't work for IL32P64.

Consider this example, compiled on a 64-bit debian:

And here's my output:


You could say that it will work if sizeof(int) equals sizeof(void*), but that doesn't have to be true either - signed integer overflow isn't exactly defined either.
Member
Posts: 690
Joined: Aug 6 2012
Gold: 275.00
Jan 25 2013 01:15am
Yeah, yeah, you are right, I've assumed sizeof(int) == sizeof(float*), so that code is platform specific and anyway it smells bad.
Btw, what is IDE on your screenshot and shell is zsh?
Member
Posts: 9,803
Joined: Jun 28 2005
Gold: 6.67
Jan 25 2013 04:23am
It's QtCreator, the best C++ IDE out there ;)
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll