type-punning Questions
7
Is it OK to cast a double array to a struct made of doubles?
struct A
{
double x;
double y;
double z;
};
int main (int argc , char ** argv)
{
double arr[3] = {1.0,2.0,3.0};
A* a = static_cas...
Radarscope asked 26/6, 2015 at 21:26
5
Solved
Is it possible in C# to turn a bool into a byte or int (or any integral type, really) without branching?
In other words, a ternary is not good enough:
var myInt = myBool ? 1 : 0; // could compile t...
Contrapuntal asked 19/11, 2019 at 13:15
2
Solved
Consider this union:
typedef union
{
void* vptr;
nullptr_t nptr;
} pun_intended;
nullptr_t is supposedly compatible with void* 1). Ok so what if we initialize the void* to some non-zero value?
p...
Coinsure asked 15/8, 2023 at 13:39
8
Solved
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 t...
Beograd asked 11/1, 2012 at 18:28
4
Say I'm working on a library called libModern. This library uses a legacy C library, called libLegacy, as an implementation strategy. libLegacy's interface looks like this:
typedef uint32_t L...
Moralize asked 13/1, 2022 at 22:55
3
Solved
I'm receiving a buffer from a network which was converted to an array of 32-bit words. I have one word which is defined as an IEEE-754 float by my interface document. I need to extract this word fr...
Skilful asked 15/2, 2018 at 9:2
2
Solved
We know that char* can alias anything: According to cppreference
Whenever an attempt is made to read or modify the stored value of an object of type DynamicType through a glvalue of type AliasedTy...
Fernand asked 1/7, 2020 at 12:4
5
Solved
This code to read a uint64_t as two uint32_t is UB due to the strict aliasing rule:
uint64_t v;
uint32_t lower = reinterpret_cast<uint32_t*>(&v)[0];
uint32_t upper = reinterpret_cast<u...
Prompt asked 10/11, 2021 at 10:15
2
In non-Cuda C++ code the current suggested practice is that type punning through a memcpy should be used rather than UB via a union. Despite it possibly causing performance issues in Debug builds, ...
Rodriques asked 31/10, 2017 at 13:52
3
Solved
I recently learned that it is Undefined Behavior to reinterpret a POD as a different POD by reinterpret_casting its address. So I'm just wondering what a potential use-case of reinterpret_cast migh...
Fanciful asked 29/7, 2021 at 12:38
4
Solved
In the following question:
What's a proper way of type-punning a float to an int and vice-versa?, the conclusion is that the way to construct doubles from integer bits and vise versa is via me...
Elene asked 14/10, 2017 at 14:46
2
Solved
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 di...
Cavazos asked 21/5, 2021 at 11:45
3
Solved
#include <iostream>
int main(int argc, char * argv[])
{
int a = 0x3f800000;
std::cout << a << std::endl;
static_assert(sizeof(float) == sizeof(int), "Oops");
flo...
Upshot asked 4/4, 2016 at 21:36
8
Solved
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
...
Colored asked 22/7, 2013 at 14:18
16
Solved
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;
...
Monmouthshire asked 22/2, 2010 at 11:17
3
Solved
I am trying to get a grasp of undefined-behavior when violating the strict aliasing rule. I have read many articles on SO in order to understand it. However, one question remains: I do not really u...
Vastha asked 23/8, 2018 at 11:16
9
I'm trying to do OOP on C (just for fun) and I've come up with a method to do data abstraction by having a struct with the public part and a larger struct with the public part first and then the pr...
Babble asked 10/3, 2015 at 16:5
3
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...
Ischium asked 28/11, 2019 at 4:45
1
Solved
Is there a way to type-pun that is valid in both C and C++? Preferably low overhead, and avoiding trivial preprocessor hacks.
In C89, I know I can do something like this:
unsigned int float_bits...
Leduc asked 2/11, 2019 at 19:33
2
Solved
std::bit_cast is apparently being introduced in c++20. and std::start_lifetime_as is proposed for c++23 (from P0593R5). As they appear to both require that the datatypes involved be trivial anyways...
Reiterate asked 6/10, 2019 at 3:38
3
Solved
In his recent talk “Type punning in modern C++” Timur Doumler said that std::bit_cast cannot be used to bit cast a float into an unsigned char[4] because C-style arrays cannot be returned from a fu...
Resonator asked 10/10, 2019 at 9:59
0
Specifically, I would like to be able to use polymorphism without heap allocation in an embedded context (thus without dynamic allocation). My concern here seems to be that accessing the base membe...
Unshackle asked 10/6, 2019 at 19:54
1
Solved
I am getting 4 bytes of data through an interface(Bluetooth, List). The data is representing IEEE 754 float (e.g. 0x3fd0a3d7, which represents approximately 1.63 as a binary32 float)
Is there a wa...
Menhaden asked 26/3, 2019 at 10:58
3
Solved
Is it legal to do a type-punning between an integer and an array of integers?
Specific code:
#include <nmmintrin.h>
#include <stdint.h>
union Uint128 {
__uint128_t uu128;
uint64_t ...
Disqualification asked 5/3, 2019 at 20:11
3
Type punning
A form of pointer aliasing where two pointers and refer to the same location in memory but represent that location as different types. The compiler will treat both "puns" as...
Banket asked 23/5, 2017 at 14:21
1 Next >
© 2022 - 2024 — McMap. All rights reserved.