problem about type-punning-Record and share programming errors


  • curiousguy
    c++ floating-point bits strict-aliasing type-punning
    I am trying to extract the bits from a float without invoking undefined behavior. Here is my first attempt:unsigned foo(float x) {unsigned* u = (unsigned*)&x;return *u; }As I understand it, this is not guaranteed to work due to strict aliasing rules, right? Does it work if a take an intermediate step with a character pointer?unsigned bar(float x) {char* c =

  • curiousguy
    c++ casting reinterpret-cast strict-aliasing type-punning
    I’ve been reading about strict aliasing quite a lot lately. The C/C++ standards say that the following code is invalid (undefined behavior to be correct), since the compiler might have the value of a cached somewhere and would not recognize that it needs to update the value when I update b;float *a; … int

  • curiousguy
    c++ c unions strict-aliasing type-punning
    In the comments of this answer it is said that it would be undefined behavior to split up an integer into their bytes using a union like follows. The code given at that place is similar though not identical to this, please give a note if have I changed undefined-behavior-relevant aspects of the code.union addr {uint8_t addr8[4];uint32_t addr32; };Up to now I thought this would be a fine approach to do things like addr = {127, 0, 0, 1}; and get the corr

  • Kristian Spangsege
    c++ double unions type-punning
    EDIT: Skip down below the horizontal rule for my newest version of the question.Assuming only that sizeof(double) * CHAR_BITS

Originally posted 2013-08-31 06:48:11.