strict-aliasing

Can an int be aliased as an unsigned int? [duplicate]

情到浓时终转凉″ 提交于 2021-02-09 11:00:26
问题 This question already has answers here : Writing to a signed integer as if it is unsigned in C++ (2 answers) Efficient way to bit-copy a signed integer to an unsigned integer (4 answers) Closed 3 years ago . Compiler generates code assuming that an int can be aliased by an unsigned int . The folowing code: int f(int& a, unsigned int& b){ a=10; b=12; return a; } int f(int& a, double& b){ a=10; b=12; return a; } generates the folowing assembly, using Clang5 (similar code is produced by GCC or

Can an int be aliased as an unsigned int? [duplicate]

回眸只為那壹抹淺笑 提交于 2021-02-09 10:58:34
问题 This question already has answers here : Writing to a signed integer as if it is unsigned in C++ (2 answers) Efficient way to bit-copy a signed integer to an unsigned integer (4 answers) Closed 3 years ago . Compiler generates code assuming that an int can be aliased by an unsigned int . The folowing code: int f(int& a, unsigned int& b){ a=10; b=12; return a; } int f(int& a, double& b){ a=10; b=12; return a; } generates the folowing assembly, using Clang5 (similar code is produced by GCC or

Can an int be aliased as an unsigned int? [duplicate]

一笑奈何 提交于 2021-02-09 10:58:28
问题 This question already has answers here : Writing to a signed integer as if it is unsigned in C++ (2 answers) Efficient way to bit-copy a signed integer to an unsigned integer (4 answers) Closed 3 years ago . Compiler generates code assuming that an int can be aliased by an unsigned int . The folowing code: int f(int& a, unsigned int& b){ a=10; b=12; return a; } int f(int& a, double& b){ a=10; b=12; return a; } generates the folowing assembly, using Clang5 (similar code is produced by GCC or

Can an int be aliased as an unsigned int? [duplicate]

走远了吗. 提交于 2021-02-09 10:58:09
问题 This question already has answers here : Writing to a signed integer as if it is unsigned in C++ (2 answers) Efficient way to bit-copy a signed integer to an unsigned integer (4 answers) Closed 3 years ago . Compiler generates code assuming that an int can be aliased by an unsigned int . The folowing code: int f(int& a, unsigned int& b){ a=10; b=12; return a; } int f(int& a, double& b){ a=10; b=12; return a; } generates the folowing assembly, using Clang5 (similar code is produced by GCC or

What rules are there for qualifiers of effective type?

不想你离开。 提交于 2021-01-28 11:32:51
问题 So I was re-reading C17 6.5/6 - 6.5/7 regarding effective type and strict aliasing, but couldn't figure out how to treat qualifiers. Some things confuse me: I always assumed that qualifiers aren't really relevant for effective type since the rules speak of lvalue access, meaning lvalue conversion that discards qualifiers. But what if the object is a pointer? Qualifiers to the pointed-at data aren't affected by lvalue conversion. Q1: What if the effective type is a pointer to qualified-type?

P1359R0 [basic.lval] wording change and aliasing rules [duplicate]

一世执手 提交于 2021-01-28 10:59:56
问题 This question already has an answer here : What happened to the “aggregate or union type that includes one of the aforementioned types” strict aliasing rule? (1 answer) Closed last year . The N4778 draft (2018) of the C++ standard contains the following section: 7.2.1 [basic.lval] 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

Aliasing through unions

偶尔善良 提交于 2021-01-27 14:16:51
问题 The 6.5(p7) has a bullet about union s and aggregate s: An object shall have its stored value accessed only by an lvalue expression that has one of the following types: [...] — an aggregate or union type that includes one of the aforementioned types among its members (including, recursively, a member of a subaggregate or contained union), or This is not quite clear what it means. Does it require at least one member or all members to satisfy the strict aliasing rule. Particularly about union s

memcpy from one type to another type. How do we access the destination afterwards?

萝らか妹 提交于 2021-01-27 12:51:34
问题 uint32_t u32 = 0; uint16_t u16[2]; static_assert(sizeof(u32) == sizeof(u16), ""); memcpy(u16, &u32, sizeof(u32)); // defined? // if defined, how to we access the data from here on? Is this defined behaviour? And, if so, what type of pointer may we use to access the target data after the memcpy ? Must we use uint16_t* , because that suitable for the declared type of u16 ? Or must we use uint32_t* , because the type of the source data (the source data copied from by memcpy ) is uint_32 ?

Allocating a struct dirent without malloc()

折月煮酒 提交于 2020-12-12 11:38:47
问题 I need to use readdir_r() to read the contents of a directory in a multithreaded program. Since the size of struct dirent is filesystem dependent, man readdir_r recommends name_max = pathconf(dirpath, _PC_NAME_MAX); if (name_max == -1) /* Limit not defined, or error */ name_max = 255; /* Take a guess */ len = offsetof(struct dirent, d_name) + name_max + 1; to find the size of the allocation needed. To allocate it entryp = malloc(len); is called, and finally readdir_r() uses it like this: