unspecified-behavior

Is dereferencing a NULL pointer considered unspecified or undefined behaviour?

假如想象 提交于 2021-01-27 20:41:27
问题 The consensus of stackoverflow questions say that it is undefined behaviour. However, I recently saw a 2016 talk by Charles Bay titled: Instruction Reordering Everywhere: The C++ 'As-If" Rule and the Role of Sequence. At 37:53 he shows the following: C++ Terms Undefined Behaviour: Lack of Constraints (order of globals initialization) Unspecified Behaviour: Constraint Violation (dereferencing NULL pointer) Now I have conflicting information. Was this a typo? Has anything changed? 回答1: The

Is there a sequence point between a function call returning an object and a method call on that object?

岁酱吖の 提交于 2019-12-29 06:41:10
问题 If I write f(x)->g(args, ...) can I rely on a sequence point after f(x) before the evaluation of args, ... ? I can see arguments both ways: §1.9.17 "When calling a function (whether or not the function is inline), there is a sequence point after the evaluation of all function arguments (if any) which takes place before execution of any expressions or statements in the function body. There is also a sequence point after the copying of a returned value and before the execution of any

Is there a sequence point between a function call returning an object and a method call on that object?

半腔热情 提交于 2019-12-29 06:41:10
问题 If I write f(x)->g(args, ...) can I rely on a sequence point after f(x) before the evaluation of args, ... ? I can see arguments both ways: §1.9.17 "When calling a function (whether or not the function is inline), there is a sequence point after the evaluation of all function arguments (if any) which takes place before execution of any expressions or statements in the function body. There is also a sequence point after the copying of a returned value and before the execution of any

Is it undefined behaviour if multiple operands in a compound expression modify the same object?

陌路散爱 提交于 2019-12-22 01:23:48
问题 I vaguely remember reading somewhere that it is undefined behaviour if multiple operands in a compound expression modify the same object. I believe an example of this UB is shown in the code below however I've compiled on g++, clang++ and visual studio and all of them print out the same values and can't seem to produce unpredictable values in different compilers. #include <iostream> int a( int& lhs ) { lhs -= 4; return lhs; } int b( int& lhs ) { lhs *= 7; return lhs; } int c( int& lhs ) { lhs

Sequence points when calling functions in C and undefined/unspecified behaviour

北城以北 提交于 2019-12-21 17:37:33
问题 I'm trying to pin down my understanding of sequence points in C -- just wanted to check something. At present, I believe that (1) is undefined whereas (2) is merely unspecified, on the basis that in (2), there are sequence points after evaluating the arguments for g and h (so we're not modifying i twice between sequence points), but the order of evaluation of the arguments of f is still unspecified. Is my understanding correct? #include <stdio.h> int g(int i) { return i; } int h(int i) {

Sequence Point ambiguity, undefined behavior?

十年热恋 提交于 2019-12-21 03:18:19
问题 Today I came across some code that exhibits different behavior on clang++ (3.7-git), g++ (4.9.2) and Visual Studio 2013. After some reduction I came up with this snippet which highlights the issue: #include <iostream> using namespace std; int len_ = -1; char *buffer(int size_) { cout << "len_: " << len_ << endl; return new char[size_]; } int main(int argc, char *argv[]) { int len = 10; buffer(len+1)[len_ = len] = '\0'; cout << "len_: " << len_ << endl; } g++ (4.9.2) gives this output: len_:

Should an empty base class affect the layout of the derived class?

≡放荡痞女 提交于 2019-12-19 19:53:12
问题 The C++ standard (quoting from draft n3242) says the following about subobjects [intro.object]: Unless an object is a bit-field or a base class subobject of zero size, the address of that object is the address of the first byte it occupies. Two distinct objects that are neither bit-fields nor base class subobjects of zero size shall have distinct addresses. Now, given the following snippet: struct empty { }; struct member: empty { }; struct derived: empty { member m; }; int main(void) {

Can I take the address of a function defined in standard library?

守給你的承諾、 提交于 2019-12-17 18:34:13
问题 Consider the following code: #include <cctype> #include <functional> #include <iostream> int main() { std::invoke(std::boolalpha, std::cout); // #1 using ctype_func = int(*)(int); char c = std::invoke(static_cast<ctype_func>(std::tolower), 'A'); // #2 std::cout << c << "\n"; } Here, the two calls to std::invoke are labeled for future reference. The expected output is: a Is the expected output guaranteed in C++20? (Note: there are two functions called tolower — one in <cctype> and the other in

In C99, is f()+g() undefined or merely unspecified?

核能气质少年 提交于 2019-12-17 07:23:09
问题 I used to think that in C99, even if the side-effects of functions f and g interfered, and although the expression f() + g() does not contain a sequence point, f and g would contain some, so the behavior would be unspecified: either f() would be called before g(), or g() before f(). I am no longer so sure. What if the compiler inlines the functions (which the compiler may decide to do even if the functions are not declared inline ) and then reorders instructions? May one get a result

Does this code from “The C++ Programming Language” 4th edition section 36.3.6 have well-defined behavior?

有些话、适合烂在心里 提交于 2019-12-17 03:28:10
问题 In Bjarne Stroustrup's The C++ Programming Language 4th edition section 36.3.6 STL-like Operations the following code is used as an example of chaining: void f2() { std::string s = "but I have heard it works even if you don't believe in it" ; s.replace(0, 4, "" ).replace( s.find( "even" ), 4, "only" ) .replace( s.find( " don't" ), 6, "" ); assert( s == "I have heard it works only if you believe in it" ) ; } The assert fails in gcc ( see it live ) and Visual Studio ( see it live ), but it does