undefined-behavior

Does casting pointers to integers define a total order on pointers?

荒凉一梦 提交于 2021-02-07 18:42:46
问题 (related to my previous question) In QT, the QMap documentation says: The key type of a QMap must provide operator<() specifying a total order . However, in qmap.h , they seem to use something similar to std::less to compare pointers: /* QMap uses qMapLessThanKey() to compare keys. The default implementation uses operator<(). For pointer types, qMapLessThanKey() casts the pointers to integers before it compares them, because operator<() is undefined on pointers that come from different memory

Does casting pointers to integers define a total order on pointers?

旧时模样 提交于 2021-02-07 18:42:28
问题 (related to my previous question) In QT, the QMap documentation says: The key type of a QMap must provide operator<() specifying a total order . However, in qmap.h , they seem to use something similar to std::less to compare pointers: /* QMap uses qMapLessThanKey() to compare keys. The default implementation uses operator<(). For pointer types, qMapLessThanKey() casts the pointers to integers before it compares them, because operator<() is undefined on pointers that come from different memory

Is UB in unevaluated context (e.g. requires-expressions) still UB?

半世苍凉 提交于 2021-02-07 05:23:24
问题 The C++ 20 draft [concept.default.init] does not precisely define default_initializable template<class T> concept default_initializable = constructible_from<T> && requires { T{}; } && is-default-initializable <T>; // exposition-only and describe what is-default-initializable should do with the following words: For a type T , is-default-initializable <T> is true if and only if the variable definition T t; is well-formed for some invented variable t; otherwise it is false. Access checking is

Is using sizeof on a variable where a type of the same name exists well defined?

扶醉桌前 提交于 2021-02-07 05:11:34
问题 Is this well defined behaviour or is it undefined / somehow else defined which foo (data type or identifier) sizeof will be operating on ? typedef int foo; int main(int argc, char *argv[]) { char foo; printf ("%u\r\n", sizeof(foo)); return 0; } If it is well defined, is there a way I could obtain the size of the datatype foo without declaring a variable of that type only to use sizeof on it? 回答1: C does not have explicit scope resolution, so identifiers (variable names, typedef names, struct

Reading data into an uninitialized char pointer variable using fgets crashes at the second read

纵饮孤独 提交于 2021-02-05 08:55:09
问题 I am aware that we cannot read data into an uninitialized char pointer using fgets. There are quite a few questions relating to this very point here on stackoverflow. All the answers point to the fact that you can't load data into an uninitialized pointer variable. The program shown in the first code snippet is able to populate the first uninitialized char pointer (*str2) using fgets but, crashes while trying to read data into the second uninitialized char pointer (*str3). I can get it to

Reading data into an uninitialized char pointer variable using fgets crashes at the second read

左心房为你撑大大i 提交于 2021-02-05 08:55:07
问题 I am aware that we cannot read data into an uninitialized char pointer using fgets. There are quite a few questions relating to this very point here on stackoverflow. All the answers point to the fact that you can't load data into an uninitialized pointer variable. The program shown in the first code snippet is able to populate the first uninitialized char pointer (*str2) using fgets but, crashes while trying to read data into the second uninitialized char pointer (*str3). I can get it to

Evaluation order of function arguments and default arguments

北慕城南 提交于 2021-02-04 21:58:29
问题 I recently ran across the following situation: #include <iostream> int *p = 0; int f() { p = new int(10); return 0; } void g(int x, int *y = p) { std::cout << y << std::endl; } int main() { g(f()); } This is quite subtle, since you usually don't expect the default arguments to change during their evaluation for the function call. I had to take a look at the assembly to spot this error. Now my question is: Is this really undefined behavior, since there aren't any guarantees concerning the

Why is PHP selecting the Random Values like that?

∥☆過路亽.° 提交于 2021-02-04 17:26:05
问题 So... I was testing something and noticed that when I run this code: $arr = str_split("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890", 1); print_r(implode(array_rand(array_flip($arr), 16))); The Output is Refresh 1: BDFIJKPTVkl12789 Refresh 2: HIJKMQWYdfmorsw3 Refresh 3: FGHMNRVYfhknouw5 Refresh 4: AFIJKRSVeiuwx579 Refresh 5: DJORYZcgijlpqry1 Refresh 6: EISWbhjmoqr45689 Refresh 7: CDEFOTXdhkloqr27 Refresh 8: AEFIKLNORSknx349 Refresh 9: DEFHJMTVZcgpstz0 Refresh 10:

Does this implementation of offsetof invoke undefined behavior? [duplicate]

强颜欢笑 提交于 2021-02-04 06:28:47
问题 This question already has answers here : Why does this implementation of offsetof() work? (8 answers) Closed 1 year ago . offsetof is defined like this in stddef.h : #define offsetof(type, member) ((size_t)&((type *)0)->member) Does this invoke undefined behavior due to the dereference of a NULL pointer? If not, why? 回答1: In normal C code, the behavior of ((size_t)&((type *)0)->member) is not specified by the C standard: First, per C 2018 6.5.2.3 4, about -> , ((type *)0)->member designates

Does this implementation of offsetof invoke undefined behavior? [duplicate]

佐手、 提交于 2021-02-04 06:28:05
问题 This question already has answers here : Why does this implementation of offsetof() work? (8 answers) Closed 1 year ago . offsetof is defined like this in stddef.h : #define offsetof(type, member) ((size_t)&((type *)0)->member) Does this invoke undefined behavior due to the dereference of a NULL pointer? If not, why? 回答1: In normal C code, the behavior of ((size_t)&((type *)0)->member) is not specified by the C standard: First, per C 2018 6.5.2.3 4, about -> , ((type *)0)->member designates