#strict aliasing

19 messages · Page 1 of 1 (latest)

bold imp
#

is this considered UB (assume sizeof(float) == sizeof(int))

int fn(float f) {
  return *((int*)((char*) &f));
}
cinder elmBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question run !howto ask.

hushed holly
#

yes

bold imp
#

so it doesnt matter if i do it through a char *?

#

aka theres no legal way to do this ?

hushed holly
#

it's ultimately through int*, the step in the middle is pointless

#

what you're doing is accessing an object of type float through a pointer to int, that's never valid

bold imp
#

alright thx

hushed holly
bold imp
#

memcpy would make it legal ?

hushed holly
#

yeah

bold imp
#

because i do it byte per byte ?

hushed holly
#

it might also be valid to use std::start_lifetime_as to begin the lifetime of an int in the storage of the float

hushed holly
#

oh wait this question is for C, so no std::start_lifetime_as then I guess

#

so just memcpy and union then, where union is only a valid approach in C

bold imp
#

neat

#

thank you

#

!solved