On Fri, Apr 18, 2008 at 3:02 PM, <[EMAIL PROTECTED]> wrote:
> > That won't work. 'buffer' goes away at the end of the function call, so
> > the result is undefined behavior. (i.e. you have a bug in your code
> > that depends on the stack not being modified when you go to read it
> > later).
> >
>
> oh yes, thanks for the hint. It worked in my case, but i can see the
> problem...was jut lucky. i guess this should be better:?
> Thanks again!
>
> float someFloatThatNeedsSwapping;
>
> floatSwap2 (&someFloatThatNeedsSwapping);
>
> void floatSwap2(float* f)
>
> {
> char* value = (char *)f;
> char buffer[ 4 ];
> buffer[0] = value[3];
> buffer[1] = value[2];
> buffer[2] = value[1];
> buffer[3] = value[0];
> *f = *(float*)&buffer;
>
> };
Nope. You're likely to hit NaN's and other weird and wonderful
artifacts of IEEE754 when handling the return value.
Just make buffer static within the function and it'll be safe to
return its address. Just don't use it in a multithreaded program and
save the contents of the return before calling the function again.
--
PJH
http://shabbleland.myminicity.com/tra