Questions tagged [type-punning]

The process of reinterpreting an object of some data type as an object of some other data type. This often involves the reinterpretation of the low-level representation of an object. This term is commonly used in the context of the C and C++ programming languages.

188 questions
861
votes
11 answers

What is the strict aliasing rule?

When asking about common undefined behavior in C, people sometimes refer to the strict aliasing rule. What are they talking about?
Benoit
  • 34,830
  • 24
  • 79
  • 113
281
votes
16 answers

Purpose of Unions in C and C++

I have used unions earlier comfortably; today I was alarmed when I read this post and came to know that this code union ARGB { uint32_t colour; struct componentsTag { uint8_t b; uint8_t g; uint8_t r; …
legends2k
  • 27,643
  • 22
  • 108
  • 196
106
votes
3 answers

Is it possible to write Quake's fast InvSqrt() function in Rust?

This is just to satisfy my own curiosity. Is there an implementation of this: float InvSqrt (float x) { float xhalf = 0.5f*x; int i = *(int*)&x; i = 0x5f3759df - (i>>1); x = *(float*)&i; x = x*(1.5f - xhalf*x*x); return x; } in…
Flyq
  • 1,158
  • 1
  • 9
  • 11
85
votes
5 answers

Unions and type-punning

I've been searching for a while, but can't find a clear answer. Lots of people say that using unions to type-pun is undefined and bad practice. Why is this? I can't see any reason why it would do anything undefined considering the memory you write…
Matthew Wilkins
  • 1,055
  • 1
  • 9
  • 12
62
votes
4 answers

Is type-punning through a union unspecified in C99, and has it become specified in C11?

A number of answers for the Stack Overflow question Getting the IEEE Single-precision bits for a float suggest using a union structure for type punning (e.g.: turning the bits of a float into a uint32_t): union { float f; uint32_t u; }…
sfstewman
  • 5,303
  • 1
  • 16
  • 25
45
votes
6 answers

C# 'unsafe' function — *(float*)(&result) vs. (float)(result)

Can anyone explain in a simple way the codes below: public unsafe static float sample(){ int result = 154 + (153 << 8) + (25 << 16) + (64 << 24); return *(float*)(&result); //don't know what for... please explain } Note: the above…
gchimuel
  • 507
  • 1
  • 5
  • 9
40
votes
8 answers

Fix for dereferencing type-punned pointer will break strict-aliasing

I'm trying to fix two warnings when compiling a specific program using GCC. The warnings are: warning: dereferencing type-punned pointer will break strict-aliasing rules [-Wstrict-aliasing] and the two culprits are: unsigned int received_size =…
BlankFrank
  • 403
  • 1
  • 4
  • 4
31
votes
2 answers

What is the modern, correct way to do type punning in C++?

It seems like there are two types of C++. The practical C++ and the language lawyer C++. In certain situations it can be useful to be able to interpret a bit pattern of one type as if it were a different type. Floating point tricks are a notable…
janekb04
  • 1,682
  • 4
  • 22
29
votes
1 answer

aligned_storage and strict aliasing

I'm currently using aligned_storage to implement an 'Optional' type similar to that of boost::optional. To accomplish this I have a class member like so: typename std::aligned_storage::value>::type t_; I use…
27
votes
2 answers

union 'punning' structs w/ "common initial sequence": Why does C (99+), but not C++, stipulate a 'visible declaration of the union type'?

Background Discussions on the mostly un-or-implementation-defined nature of type-punning via a union typically quote the following bits, here via @ecatmur ( https://stackoverflow.com/a/31557852/2757035 ), on an exemption for standard-layout structs…
underscore_d
  • 5,331
  • 3
  • 29
  • 56
27
votes
8 answers

What's a proper way of type-punning a float to an int and vice-versa?

The code below performs a fast inverse square root operation by some bit hacks. The algorithm was probably developed by Silicon Graphics in early 1990's and it's appeared in Quake 3 too. more info However I get the following warning from GCC C++…
plasmacel
  • 7,355
  • 5
  • 42
  • 87
27
votes
3 answers

Aliasing T* with char* is allowed. Is it also allowed the other way around?

Note: This question has been renamed and reduced to make it more focused and readable. Most of the comments refer to the old text. According to the standard, objects of different type may not share the same memory location. So this would not be…
StackedCrooked
  • 32,392
  • 40
  • 137
  • 267
25
votes
5 answers

Reusing a float buffer for doubles without undefined behaviour

In one particular C++ function, I happen to have a pointer to a big buffer of floats that I want to temporarily use to store half the number of doubles. Is there a method to use this buffer as scratch space for storing the doubles, which is also…
André Offringa
  • 362
  • 2
  • 7
23
votes
4 answers

Opinions on type-punning in C++?

I'm curious about conventions for type-punning pointers/arrays in C++. Here's the use case I have at the moment: Compute a simple 32-bit checksum over a binary blob of data by treating it as an array of 32-bit integers (we know its total length is…
Tom
  • 10,273
  • 3
  • 39
  • 49
22
votes
4 answers

float bits and strict aliasing

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…
fredoverflow
  • 237,063
  • 85
  • 359
  • 638
1
2 3
12 13