undefined-behavior

Is using malloc for int undefined behavior until C++20

戏子无情 提交于 2020-08-21 03:40:04
问题 I was told that the following code has undefined behavior until C++20: int *p = (int*)malloc(sizeof(int)); *p = 10; Is that true? The argument was that the lifetime of the int object is not started before assigning the value to it (P0593R6). To fix the problem, placement new should be used: int *p = (int*)malloc(sizeof(int)); new (p) int; *p = 10; Do we really have to call a default constructor that is trivial to start the lifetime of the object? At the same time, the code does not have

Is using malloc for int undefined behavior until C++20

自古美人都是妖i 提交于 2020-08-21 03:40:03
问题 I was told that the following code has undefined behavior until C++20: int *p = (int*)malloc(sizeof(int)); *p = 10; Is that true? The argument was that the lifetime of the int object is not started before assigning the value to it (P0593R6). To fix the problem, placement new should be used: int *p = (int*)malloc(sizeof(int)); new (p) int; *p = 10; Do we really have to call a default constructor that is trivial to start the lifetime of the object? At the same time, the code does not have

When is there an UB because the best overload match was not found by ADL at the point of instantiation?

℡╲_俬逩灬. 提交于 2020-08-02 07:47:32
问题 When a function body is instantiated, dependent function call overload resolution should find the best match in associated namespace through ADL, otherwise the behavior is undefined, [temp.dep.candidate]§1 If the call would be ill-formed or would find a better match had the lookup within the associated namespaces considered all the function declarations with external linkage introduced in those namespaces in all translation units, not just considering those declarations found in the template

How can a literal 0 and 0 as a variable yield different behavior with the function __builtin_clz?

回眸只為那壹抹淺笑 提交于 2020-07-20 14:06:31
问题 There's only 1 circumstance where __builtin_clz gives the wrong answer. I'm curious what's causing that behavior. When I use the literal value 0 I always get 32 as expected. But 0 as a variable yields 31. Why does the method of storing the value 0 matter? I've taken an architecture class but don't understand the diffed assembly. It looks like when given the literal value 0, the assembly somehow always has the correct answer of 32 hard coded even without optimizations. And the method for

Overlapping memory with sprintf(snprintf)

妖精的绣舞 提交于 2020-07-10 11:41:32
问题 Edit: What about if we had this char value_arr[8]; // value_arr is set to some value snprintf(value_arr, 8, "%d", *value_arr); is this behavior defined? Let's say for some ungainly reason I have char value_arr[8]; // value_arr is set to some value int* value_i = reinterpret_cast<int*>(value_arr); snprintf(value_arr, 8, "%d", *value_i); // the behaviour in question Is there a guarantee that, for example, if *value_i = 7, then value_arr will take on the value of "7". Is this behavior defined?

Overlapping memory with sprintf(snprintf)

社会主义新天地 提交于 2020-07-10 11:40:29
问题 Edit: What about if we had this char value_arr[8]; // value_arr is set to some value snprintf(value_arr, 8, "%d", *value_arr); is this behavior defined? Let's say for some ungainly reason I have char value_arr[8]; // value_arr is set to some value int* value_i = reinterpret_cast<int*>(value_arr); snprintf(value_arr, 8, "%d", *value_i); // the behaviour in question Is there a guarantee that, for example, if *value_i = 7, then value_arr will take on the value of "7". Is this behavior defined?

Preincrement vs postincrement in terms of sequence points

久未见 提交于 2020-07-09 02:34:39
问题 In this answer there're some examples of well-defined and undefined expressions. I'm particularly interested in two of them: (6) i = i++ + 1; // Undefined Behaviour (7) i = ++i + 1; // Well-defined Behaviour This means that there's a difference between pre-increment and post-increment in terms of sequence points and well defined /unspecified/undefined behavior, but I don't understand where this difference comes from. In standard draft ( N4618 ) there's an example of code ([intro.execution],

atomic_ref when external underlying type is not aligned as requested

╄→尐↘猪︶ㄣ 提交于 2020-06-27 18:35:08
问题 I read on p0019r8 the following: atomic_ref(T& obj); Requires : The referenced object shall be aligned to required_alignment . cppreference interprets this as UB when not aligned: The behavior is undefined if obj is not aligned to required_alignment. So how would you expect from an implementation to handle it? And implementation can check compile-time alignof , but in reality a type might be aligned more that alignof . An implementation can interpret pointer bits and check runtime alignment,