strict-aliasing Questions

2

Solved

Twenty plus years ago, I would have (and didn't) think anything of doing binary I/O with POD structs: struct S { std::uint32_t x; std::uint16_t y; }; S s; read(fd, &s, sizeof(s)); // assume th...
Nemhauser asked 27/7, 2019 at 17:3

1

Solved

So recently I have been working with Vulkan-Hpp (The official c++ bindings of Vulkan Api, Github Link). Looking into the source, I have found that they create wrapper classes around native Vulkan ...
Shout asked 11/7, 2019 at 9:21

1

Solved

Previously, in basic.lval, there was this bullet point: an aggregate or union type that includes one of the aforementioned types among its elements or non-static data members (including, recursi...
Freitag asked 3/7, 2019 at 22:12

3

Do the C++20's strict aliasing rules [basic.lval]/11 arbitrarily allow following... cast between char* and char8_t* string str = "string"; u8string u8str { (char8_t*) &*str.data() }; // c++...
Siberia asked 2/6, 2019 at 12:57

1

In an attempt to get a better understand of how pointer aliasing invariants manifested during optimization, I plugged some code into the renowned Compiler Explorer, which I'll repeat here: #includ...
Merce asked 12/5, 2019 at 15:41

5

Solved

I have a problem understanding what can and cannot be done using unions with GCC. I read the questions (in particular here and here) about it but they focus the C++ standard, I feel there's a misma...
Armalda asked 19/2, 2019 at 8:54

1

Solved

I'm trying to understand strict aliasing rule as defined in 6.5(p6): If a value is stored into an object having no declared type through an lvalue having a type that is not a character type, th...
Thromboplastin asked 17/2, 2019 at 9:9

4

Solved

Prompted by this question: The C11 standard states that a pointer to a union can be converted to a pointer to each of its members. From Section 6.7.2.1p17: The size of a union is sufficient to ...
Singleminded asked 4/2, 2019 at 14:52

4

Solved

I am currently wondering about the rationale behind the strict aliasing rule. I understand that certain aliasing is not allowed in C and that the intention is to allow optimizations, but I am surpr...
Griffie asked 28/12, 2018 at 12:31

1

In the c++ standard, in [basic.lval]/11.6 says: If a program attempts to access the stored value of an object through a glvalue of other than one of the following types the behavior is undefined:[...
Bozen asked 5/11, 2018 at 9:25

2

Solved

I'm reading notes about reinterpret_cast and it's aliasing rules ( http://en.cppreference.com/w/cpp/language/reinterpret_cast ). I wrote that code: struct A { int t; }; char *buf = new char[siz...
Diamagnetism asked 24/7, 2015 at 16:5

1

Solved

The following code gives me warning in gcc that I break strict aliasing rules: struct Base { int field = 2; }; template <typename T> struct Specialization: public Base { void method() { ...
Antrim asked 13/12, 2018 at 15:44

2

In the c++ standard, in [basic.lval]/11.6 says: If a program attempts to access the stored value of an object through a glvalue of other than one of the following types the behavior is undefined...

1

Solved

Is this code well-defined behavior, in terms of strict aliasing? _Bool* array = malloc(n); memset(array, 0xFF, n); _Bool x = array[0]; The rule of effective type has special cases for memcpy and...
Bacteriophage asked 6/11, 2018 at 12:21

5

Solved

It is common to assign pointers with allocations using an implicit function-return void * conversion, just like malloc()'s: void *malloc(size_t size); int *pi = malloc(sizeof *pi); I would like ...
Extine asked 2/6, 2014 at 19:46

2

Is it defined behavior to placement-new a trivially destructible base object of a derived? struct base { int& ref; }; struct derived : public base { complicated_object complicated; derived(i...
Transpontine asked 19/10, 2018 at 0:18

1

Solved

I was trying to write some macros for type safe use of _Bool and then stress test my code. For evil testing purposes, I came up with this dirty hack: _Bool b=0; *(unsigned char*)&b = 42; Giv...
Nicolette asked 4/9, 2018 at 10:9

3

Solved

This is a follow up to this other question about memory re-use. As the original question was about a specific implementation, the answer was related to that specific implementation. So I wonder wh...
Fidge asked 20/8, 2018 at 13:37

3

Solved

I was reading about strict aliasing, but its still kinda foggy and I am never sure where is the line of defined / undefined behaviour. The most detailed post i found concentrates on C. So it would ...
Enscroll asked 23/8, 2018 at 9:38

2

Solved

According to the standard, it is always undefined behavior in C++ to make, for example, a float* point to the same memory location as a int*, and then read/write from them. In the application I ha...
Skier asked 20/8, 2018 at 12:2

3

Solved

With these definitions: struct My_Header { uintptr_t bits; } struct Foo_Type { struct My_Header header; int x; } struct Foo_Type *foo = ...; struct Bar_Type { struct My_Header header; float x; }...
Narva asked 14/8, 2018 at 16:34

5

Solved

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 scra...
Tijuanatike asked 11/7, 2018 at 15:35

3

Solved

I've been using std::memcpy to circumvent strict aliasing for a long time. For example, inspecting a float, like this: float f = ...; uint32_t i; static_assert(sizeof(f)==sizeof(i)); std::memcpy(...

11

Solved

When asking about common undefined behavior in C, people sometimes refer to the strict aliasing rule. What are they talking about?
Jemine asked 19/9, 2008 at 1:30

2

Solved

Suppose I have a chunk of dynamically allocated data: void* allocate (size_t n) { void* foo = malloc(n); ... return foo; } I wish to use the data pointed at by foo as a special type, type_t. ...
Hollo asked 25/6, 2018 at 8:37

© 2022 - 2024 — McMap. All rights reserved.