Sudhakar
Sudhakar

Reputation: 93

How to resolve warning: dereferencing type-punned pointer will break strict-aliasing rules

    #define HTON_I32(x) htonl(x)
inline float  __HTON_F32(float  x)
{
    int i = HTON_I32(*((int *)(&x)));
    return (*((float *)(&i)));
}

How to resolve warning dereferencing type-punned pointer will break strict-aliasing rules in the above code

Upvotes: 5

Views: 2672

Answers (2)

Christoph
Christoph

Reputation: 169713

You can use unions for type-punning, which takes care of possible alignment and aliasing issues (C99:TC3 explicitly mentions that this is indeed legal):

#include <stdint.h>

inline float __HTON_F32(float x) {
    union { float as_float; int32_t as_int; } value = { x };
    value.as_int = HTON_I32(value.as_int);
    return value.as_float;
}

Upvotes: 1

Stephen Canon
Stephen Canon

Reputation: 106287

Eliminate the type-punning, and replace it with something that isn't fragile in the face of aliasing:

#include <string.h>

inline float __HTON_F32(float x) {
    int i;
    memcpy(&i, &x, sizeof x);
    i = HTON_I32(i);
    memcpy(&x, &i, sizeof x);
    return x;
}

Reasonable optimizing compilers will lower the memcpy calls, and generate equivalent (sometimes better) code to what you get from type-punning.

Another common solution you will see involves unions. All of these solutions assume that sizeof(int) == sizeof(float). You may want to add an assert to that effect.

Upvotes: 9

Related Questions