strict-aliasing Questions

1

Solved

On Windows, wchar_t is a UTF-16(LE) formatted character, which is -- for the most part -- equivalent to char16_t. However, these two character types are still distinct types in the C++ type-system ...
Collaborative asked 2/2, 2022 at 19:46

5

It is clear to me that the C standard forbids (does not define the behavior of) this program, but it is not clear why it has to be this way. Why are the aliasing rules such that one cannot write th...
Neural asked 17/1, 2022 at 13:57

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

5

Solved

WG14 member Jens Gustedt says in a blog post on strict aliasing rules: Character arrays must not be reinterpreted as objects of other types. Is that, in fact, true? (I guess the corresponding lan...

1

Solved

Violating strict-aliasing rules yields undefined behavior, e.g. when sending a struct over the network into a char buffer, and then that char pointer is C-style/reinterpret_cast casted to a struct ...
Acosta asked 30/5, 2021 at 21:17

2

Solved

Consider this code: void f(char * ptr) { auto int_ptr = reinterpret_cast<int*>(ptr); // <---- line of interest // use int_ptr ... } void example_1() { int i = 10; f(reinterpret_cas...

3

Solved

This question is an extension of what I have asked before. However, after a period of time, I find that some of my concepts about Conversion Behavior between Two Pointers are still ambiguous. To fa...
Coan asked 15/4, 2021 at 14:35

1

Solved

Suppose we have the following code: #include <stdio.h> #include <stdint.h> int main() { uint16_t a[] = { 1, 2, 3, 4 }; const size_t n = sizeof(a) / sizeof(uint16_t); for (size_t i...
Terminology asked 8/3, 2021 at 15:56

3

Solved

I've been writing embedded C code for many years now, and the newer generations of compilers and optimizations have certainly gotten a lot better with respect to their ability to warn about questio...
Knap asked 1/3, 2021 at 18:41

5

Solved

Recently I stumbled over a comparison between Rust and C and they use the following code: bool f(int* a, const int* b) { *a = 2; int ret = *b; *a = 3; return ret != 0; } In Rust (same code, bu...
Planetarium asked 1/2, 2021 at 13:32

2

Solved

Update 2020-12-11: Thanks @"Some programmer dude" for the suggestion in the comment. My underlying problem is that our team is implementing a dynamic type storage engine. We allocate mult...
Yowl asked 10/12, 2020 at 18:31

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 ...

1

Solved

I have stumbled upon a reddit thread in which a user has found an interesting detail of the C++ standard. The thread has not spawned much constructive discussion, therefore I will retell my underst...
Anibalanica asked 3/9, 2020 at 12:57

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

2

Premise I have a blob of binary data in memory, represented as a char* (maybe read from a file, or transmitted over the network). I know that it contains a UTF8-encoded text field of a certain len...
Rubstone asked 11/8, 2020 at 18:40

3

Solved

When I compile this sample code using g++, I get this warning: warning: dereferencing type-punned pointer will break strict-aliasing rules [-Wstrict-aliasing] The code: #include <iostream&...
Phenomenology asked 18/11, 2014 at 20:49

3

Solved

After reading this, I have a similar question like this one, wondering how a memory allocator can work without violating the strict aliasing rules. But I am not wondering about re-using freed memor...
Gamboge asked 12/4, 2020 at 20:5

3

Solved

Sample code: struct S { int x; }; int func() { S s{2}; return (int &)s; // Equivalent to *reinterpret_cast<int *>(&s) } I believe this is common and considered acceptable. The s...
Demote asked 17/5, 2018 at 4:6

1

Solved

Given the following code #include <cassert> #include <climits> #include <cstdint> #include <iostream> static_assert(CHAR_BIT == 8, "A byte does not consist of 8 bits"); v...
Discoloration asked 16/10, 2019 at 12:38

2

Solved

Does the following example violates strict aliasing rule? In file a.c extern func_takes_word(uint32_t word); void func(void *obj, size_t size_in_words) { for (int i = 0; i < size_in_words; i...
Illiquid asked 4/9, 2019 at 16:31

3

Solved

I wish to call fftw's in-place real-to-complex transform function, which has the following signature: fftw_plan fftw_plan_dft_r2c_1d( int n, // transform length double* in, // pointer to input a...
Etheline asked 1/9, 2019 at 21:42

1

Solved

In C++ there is an aliasing loophole which allows the object representation of any object to be read or written through some pointers of character type. Does this apply only to char and unsigned c...
Asti asked 26/8, 2019 at 7:56

4

Solved

I can't explain the execution behavior of this program: #include <string> #include <cstdlib> #include <stdio.h> typedef char u8; typedef unsigned short u16; size_t f(u8...
Braunite asked 17/10, 2017 at 12:48

3

Solved

A popular macro-based generic implementation of a vector in C (https://github.com/eteran/c-vector/blob/master/vector.h) uses the following memory layout. +------+----------+---------+ | size | ca...
Gravel asked 29/7, 2019 at 19:41

© 2022 - 2024 — McMap. All rights reserved.