language-lawyer

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?

How are multiple prior declarations resolved for a new declaration with extern?

て烟熏妆下的殇ゞ 提交于 2020-07-10 03:11:51
问题 What should the third x refer to in: #include <stdio.h> static char x = '1'; int main(void) { char x = '2'; { extern char x; printf("%c\n", x); } } This arose in this answer, and: In Apple LLVM 9.1.0 clang-902-0.39.2, the x of extern char x refers to the first x , and “1” is printed. GCC 8.2 does not accept this source text., complaining: “error: variable previously declared 'static' redeclared 'extern'”. C 2018 6.2.2 4 says: For an identifier declared with the storage-class specifier extern

In the C++ standard does well-formed means that the code compiles?

随声附和 提交于 2020-07-09 12:12:19
问题 The C++ standards defines well-formed programs as C ++ program constructed according to the syntax rules, diagnosable semantic rules, and the one-definition rule I am wondering if all well-formed program compile or not (if it is not the case, what types of error make the difference between a well-formed program and a compilable problem). For example would a program containing ambiguity errors considered as well-formed? 回答1: A well-formed program can have undefined behaviour. It's in a note,

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],

Is clamping on iterators valid

妖精的绣舞 提交于 2020-07-08 11:49:02
问题 I found the following in actual production code. My suspicion is that it actually has undefined behavior into it, however, I couldn't find the related info on cppreference. Can you confirm this is UB or valid code and why this is UB/valid (preferably with a quote of the standard)? #include <vector> int main(int, char **) { auto v = std::vector<int>({1,2,3,4,5}); auto begin = v.begin(); auto outOfRange = begin + 10; auto end = v.end(); auto clamped = std::min(outOfRange, end); return (clamped

Is reinterpret_cast<char*>(myTypePtr) assumed to point to an array?

牧云@^-^@ 提交于 2020-07-08 01:12:46
问题 We know that char* can alias anything: According to cppreference Whenever an attempt is made to read or modify the stored value of an object of type DynamicType through a glvalue of type AliasedType, the behavior is undefined unless one of the following is true: [...] AliasedType is std::byte , char , or unsigned char : this permits examination of the object representation of any object as an array of bytes . [...] The statement in boldface is not present in n4659 draft [6.10, (8.8)]. Since

Is reinterpret_cast<char*>(myTypePtr) assumed to point to an array?

喜夏-厌秋 提交于 2020-07-08 01:12:00
问题 We know that char* can alias anything: According to cppreference Whenever an attempt is made to read or modify the stored value of an object of type DynamicType through a glvalue of type AliasedType, the behavior is undefined unless one of the following is true: [...] AliasedType is std::byte , char , or unsigned char : this permits examination of the object representation of any object as an array of bytes . [...] The statement in boldface is not present in n4659 draft [6.10, (8.8)]. Since

How is std::atomic<T>::notify_all ordered?

空扰寡人 提交于 2020-07-06 13:55:48
问题 I expect the below program not to hang. If (2) and (3) are observed in reverse order in (1), it may hang due to lost notification: #include <atomic> #include <chrono> #include <thread> int main() { std::atomic<bool> go{ false }; std::thread thd([&go] { go.wait(false, std::memory_order_relaxed); // (1) }); std::this_thread::sleep_for(std::chrono::milliseconds(400)); go.store(true, std::memory_order_relaxed); // (2) go.notify_all(); // (3) thd.join(); return 0; } So the question is what would

Using cout in the constructor of a class that is included in another class as a static member

杀马特。学长 韩版系。学妹 提交于 2020-07-06 10:52:13
问题 Following code #include <iostream> struct A { A() { std::cout << std::endl; } }; struct B { static inline A a; }; int main() { } succeeds after compiling with gcc, but crashes with segmentation fault after compiling with clang. Is the code not standard or is clang wrong? https://godbolt.org/z/tEvfrW 回答1: Cppreference on std::ios_base::Init reads: The header <iostream> behaves as if it defines (directly or indirectly) an instance of std::ios_base::Init with static storage duration: this makes

What pointer values are well-defined to compute?

时间秒杀一切 提交于 2020-07-05 11:06:04
问题 I was under the impression that while dereferencing pointers that don't point to a valid object is UB, simply computing such pointers is fine. However, if I'm understanding expr.add[4] correctly, that's not the case. So which of these pointer computations are well-defined? int a = 42; int *p = &a; p; // valid, and obviously ok p++; // invalid, but ok, because one past the end of 'array' containing 1 element? p++; // UB ? How about this case? int *p = nullptr; p; // invalid, and obviously ok