undefined-behavior

(Where) Does clang document implementation-defined behavior?

喜夏-厌秋 提交于 2021-01-27 01:40:20
问题 Implementation-defined behaviors in C are unspecified behaviors for which each conforming implementation must document its choice. I found such documentations easily for gcc here or Microsoft C here, but I can't find any such documentation for clang. Am I searching wrong or is there no such thing? 回答1: This ticket https://bugs.llvm.org/show_bug.cgi?id=11272 is still opened (for many years now) so it seems that clang doesn't explicitly specify implementation defined behaviour. For most cases I

FP: invalid operation: contradiction between C (UB) and IEEE 754 (WDB)?

北城以北 提交于 2021-01-07 01:06:06
问题 N2479 C17..C2x working draft — February 5, 2020 ISO/IEC 9899:202x (E): 6.3.1.4 Real floating and integer: 1 When a finite value of standard floating type is converted to an integer type other than _Bool, the fractional part is discarded (i.e., the value is truncated toward zero). If the value of the integral part cannot be represented by the integer type, the behavior is undefined . IEEE 754-2019: 5.8 Details of conversions from floating-point to integer formats: When a NaN or infinite

Why is type punning considered UB?

可紊 提交于 2021-01-04 03:16:05
问题 Imagine this: uint64_t x = *(uint64_t *)((unsigned char[8]){'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'}); I have read that type puns like that are undefined behavior. Why? I am literally, reinterpreting 8 bytes of bytes into an 8 byte integer. I don't see how that's different from a union except the type pun being undefined behavior and union s not being? I asked a fellow programmer in person and they said that if you're doing it, either you know what you're doing very well , or you're making a

Why is type punning considered UB?

穿精又带淫゛_ 提交于 2021-01-04 03:14:25
问题 Imagine this: uint64_t x = *(uint64_t *)((unsigned char[8]){'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'}); I have read that type puns like that are undefined behavior. Why? I am literally, reinterpreting 8 bytes of bytes into an 8 byte integer. I don't see how that's different from a union except the type pun being undefined behavior and union s not being? I asked a fellow programmer in person and they said that if you're doing it, either you know what you're doing very well , or you're making a

Why is type punning considered UB?

别等时光非礼了梦想. 提交于 2021-01-04 03:13:25
问题 Imagine this: uint64_t x = *(uint64_t *)((unsigned char[8]){'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'}); I have read that type puns like that are undefined behavior. Why? I am literally, reinterpreting 8 bytes of bytes into an 8 byte integer. I don't see how that's different from a union except the type pun being undefined behavior and union s not being? I asked a fellow programmer in person and they said that if you're doing it, either you know what you're doing very well , or you're making a

How to use bitfields that make up a sorting key without falling into UB?

坚强是说给别人听的谎言 提交于 2020-12-13 04:55:26
问题 Let's say I want to have the following bitfield: struct SortingKey { uint8_t a: 2; uint8_t b: 4; uint8_t c: 2; } To use a simple integer comparison, I may need to wrap it into an union like this and use value for sorting: union UnionSortKey { SortingKey key; uint8_t value; } However, in C++, reading an inactive union member is Undefined Behaviour. How can I guarantee that I do not fall into UB but keeping a simple integer comparison? 回答1: You can't use union for type punning, In C++20, you

Accessing struct data members via pointer arithmetic

不羁岁月 提交于 2020-12-05 05:13:20
问题 If I have a simple tensor class like this struct Tensor { double XX, XY, XZ; double YX, YY, YZ; double ZX, ZY, ZZ; } Is it undefined behavior to use pointer-arithmetic (see below) to access its elements? double& Tensor::operator[](int i) { assert(i < 9); return (&XX)[i]; } 回答1: Yes, it is undefined behavior. The data members are not in an array, and thus are NOT guaranteed to be stored back-to-back in contiguous memory, as pointer arithmetic would require. There may be indeterminate padding