strict-aliasing

Does reinterpret_casting std::aligned_storage* to T* without std::launder violate strict-aliasing rules? [duplicate]

只愿长相守 提交于 2019-12-19 04:13:13
问题 This question already has answers here : Does this really break strict-aliasing rules? (2 answers) Closed 2 years ago . The following example comes from std::aligned_storage page of cppreference.com: #include <iostream> #include <type_traits> #include <string> template<class T, std::size_t N> class static_vector { // properly aligned uninitialized storage for N T's typename std::aligned_storage<sizeof(T), alignof(T)>::type data[N]; std::size_t m_size = 0; public: // Create an object in

easy struct inheritance & pseudo-polymorphism vs strict aliasing

て烟熏妆下的殇ゞ 提交于 2019-12-19 02:51:30
问题 If anybody answers my question, please don't tell me to use C++ . So, I'm making a small library in C that uses an object-oriented approach. I chose to use the less-common of the two main approaches to inheritance in C: copying the members of the base type to the beginning of the derived type. Something like this: struct base { int a; int b; char c; }; struct derived { int a; int b; char c; unsigned int d; void (*virtual_method)(int, char); }; This approach is less popular than the other one

easy struct inheritance & pseudo-polymorphism vs strict aliasing

﹥>﹥吖頭↗ 提交于 2019-12-19 02:50:06
问题 If anybody answers my question, please don't tell me to use C++ . So, I'm making a small library in C that uses an object-oriented approach. I chose to use the less-common of the two main approaches to inheritance in C: copying the members of the base type to the beginning of the derived type. Something like this: struct base { int a; int b; char c; }; struct derived { int a; int b; char c; unsigned int d; void (*virtual_method)(int, char); }; This approach is less popular than the other one

Understanding restrict qualifier by examples

。_饼干妹妹 提交于 2019-12-18 14:49:12
问题 The restrict keyword's behavior is defined in C99 by 6.7.3.1: Let D be a declaration of an ordinary identifier that provides a means of designating an object P as a restrict-qualified pointer to type T. If D appears inside a block and does not have storage class extern, let B denote the block. If D appears in the list of parameter declarations of a function definition, let B denote the associated block. Otherwise, let B denote the block of main (or the block of whatever function is called at

Is it UB to cast away const and read value? [duplicate]

筅森魡賤 提交于 2019-12-18 12:35:13
问题 This question already has an answer here : Does removing const from a pointer-to-const obey strict aliasing in C, and refer to the same object? (1 answer) Closed last year . Clarification: My question is: Is it UB to use an lvalue of type int to access an object of effective type const int ? This question has two code samples which use an lvalue of type int to access an object of effective type const int , and my intent is to achieve this with as little distraction as possible. If there is

In C++, should I bother to cache variables, or let the compiler do the optimization? (Aliasing)

大憨熊 提交于 2019-12-18 09:58:53
问题 Consider the following code ( p is of type unsigned char* and bitmap->width is of some integer type, exactly which is unknown and depends on which version of some external library we're using): for (unsigned x = 0; x < static_cast<unsigned>(bitmap->width); ++x) { *p++ = 0xAA; *p++ = 0xBB; *p++ = 0xCC; } Is it worth optimizing it [..] Could there be a case where this could yield more efficient results by writing: unsigned width(static_cast<unsigned>(bitmap->width)); for (unsigned x = 0; x <

Aliasing of multi-dimensional arrays

Deadly 提交于 2019-12-18 07:12:01
问题 It is well known that a 2D array is an array of arrays, and that it is required by standard to be a contiguously allocated nonempty set of objects (6.2.5 Types §20) - object being a 1D array here. It is also well known that for all common implementations the following equality is true for T arr2d[X][Y] where T is a type and X and Y integral constants: (char *) &arr2d[i][j] == (char *) &arr2d[0][0] + i * Y * sizeof(T) + j * sizeof(T) The above let think that it could be allowed to alias a 2D

Type punning a struct in C and C++ via a union

安稳与你 提交于 2019-12-18 04:40:15
问题 I've compiled this in gcc and g++ with pedantic and I don't get a warning in either one: #include <stdio.h> #include <stdlib.h> #include <string.h> struct a { struct a *next; int i; }; struct b { struct b *next; int i; }; struct c { int x, x2, x3; union { struct a a; struct b b; } u; }; void foo(struct b *bar) { bar->next->i = 9; return; } int main(int argc, char *argv[]) { struct c c; memset(&c, 0, sizeof c); c.u.a.next = (struct a *)calloc(1, sizeof(struct a)); foo(&c.u.b); printf("%d\n", c

Is it a strict aliasing violation to alias a struct as its first member?

十年热恋 提交于 2019-12-18 03:03:47
问题 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 standard does guarantee that there is no initial padding in the struct. However this case is not listed in the strict aliasing rule (C++17 [basic.lval]/11): 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: (11.1) the

C memory allocator and strict aliasing

爱⌒轻易说出口 提交于 2019-12-17 18:58:04
问题 even after reading quite a bit about the strict-aliasing rules I am still confused. As far as I have understood this, it is impossible to implement a sane memory allocator that follows these rules, because malloc can never reuse freed memory, as the memory could be used to store different types at each allocation. Clearly this cannot be right. What am I missing? How do you implement an allocator (or a memory pool) that follows strict-aliasing? Thanks. Edit: Let me clarify my question with a