strict-aliasing

Strict aliasing in Rust?

随声附和 提交于 2019-12-04 01:28:09
My understanding is that the following code has undefined behaviour in C++ due to something called "strict aliasing rule". #include <cstdint> enum Foo : int16_t {}; void test(Foo& foo) { reinterpret_cast<int16_t&>(foo) = 42; } In particular, a C++ compiler may omit the assignment altogether because it is allowed to assume that the int16_t& returned by the reinterpret_cast doesn't point to the same memory as foo (of type Foo& ) because their types are different. My question is, does Rust have something akin to the C++ "strict aliasing rule"? In other words, does the following, equivalent Rust

Is the strict aliasing rule incorrectly specified?

核能气质少年 提交于 2019-12-03 19:06:51
问题 As previously established, a union of the form union some_union { type_a member_a; type_b member_b; ... }; with n members comprises n + 1 objects in overlapping storage: One object for the union itself and one object for each union member. It is clear, that you may freely read and write to any union member in any order, even if reading a union member that was not the last one written to. The strict aliasing rule is never violated, as the lvalue through which you access the storage has the

Is std::memcpy between different trivially copyable types undefined behavior?

空扰寡人 提交于 2019-12-03 18:36:02
问题 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(&i, &f, sizeof(i)); // use i to extract f's sign, exponent & significand However, this time, I've checked the standard, I haven't found anything that validates this. All I found is this: For any object (other than a potentially-overlapping subobject) of trivially copyable type T, whether or not the

Berkley Sockets, breaking aliasing rules?

久未见 提交于 2019-12-03 16:32:42
Im asking my self, can i use the BSD sockets with strict aliasing on, without getting undefined behaviour by compiling with gcc? bind(sdListen, (struct sockaddr*)&sockaddr_inIdentifier, sizeof(sockaddr_inIdentifier)) This line of code breaks the strict aliasing rule as far as i know (and gcc gives me the same warning). so is there a plan b, of using the sockets in O3 mode without turning strictaliasing of? And of course without breaking the rule? or do i have to get an own socket system running that will be runnable on all systems/compilers? The cast itself in that line does not break the

Is it legal to reuse memory from a fundamental type array for a different (yet still fundamental) type array

故事扮演 提交于 2019-12-03 16:05:00
问题 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 whether, it is legal in a conformant implementation to re-use the memory of an array of a fundamental type for an array of a different type provided: both types are fundamental type and as such have trivial dtor and default ctor both types have same size and alignment requirement I ended with the

Does access through pointer change strict aliasing semantics?

*爱你&永不变心* 提交于 2019-12-03 16:01:02
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; } struct Bar_Type *bar = ...; Is it correct to say that this C code ( "case one" ): foo->header.bits = 1020; ...is actually different semantically from this code ( "case two" ): struct My_Header *alias = &foo->header; alias->bits = 1020; My understanding is that they should be different: Case One considers the assignment unable to affect the header in a Bar_Type. It only is seen as being able to

Can an object have more than one effective type?

痴心易碎 提交于 2019-12-03 15:50:32
Consider the following code on a platform where the ABI does not insert padding into unions: union { int xi; } x; x.xi = 1; I believe that the second line exhibits undefined behaviour as it violates the strict aliasing rule: The object referred to by x.xi is the same object as the object referred to by x . Both are the same region of storage and the term object is defined in ISO 9899:2011 §3.15 as: object 1 region of data storage in the execution environment, the contents of which can represent values 2 NOTE When referenced, an object may be interpreted as having a particular type; see 6.3.2.1

Does giving data an effective type count as a side-effect?

十年热恋 提交于 2019-12-03 14:08:58
问题 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 . But I want to do this later, and not during allocation. In order to give the allocated data an effective type , I can therefore do something like: void* allocate (size_t n) { void* foo = malloc(n); (void) *(type_t*)foo; ... return foo } As per C11 6.5/6, this lvalue access should make the effective type

Does inheritance via unwinding violate strict aliasing rule?

≡放荡痞女 提交于 2019-12-03 13:26:37
I have a struct X which inherits from struct Base. However, in my current setup, due to alignment, size of X is 24B: typedef struct { double_t a; int8_t b; } Base; typedef struct { Base base; int8_t c; } X; In order to save the memory, I'd like to unwind the Base struct, so I created struct Y which contains fields from Base (in the same order, always at the beginning of the struct), so the size of the struct is 16B: typedef struct { double_t base_a; int8_t base_b; int8_t c; } Y; Then I'm going to use instance of struct Y in a method which expects a pointer to Base struct: void print_base(Base*

Generic char[] based storage and avoiding strict-aliasing related UB

早过忘川 提交于 2019-12-03 11:51:13
I'm trying to build a class template that packs a bunch of types in a suitably large char array, and allows access to the data as individual correctly typed references. Now, according to the standard this can lead to strict-aliasing violation, and hence undefined behavior, as we're accessing the char[] data via an object that is not compatible with it. Specifically, the standard states: 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: the dynamic type of the object, a cv-qualified version of the