unspecified-behavior

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

会有一股神秘感。 提交于 2019-12-01 18:17:41
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) { printf("%d", sizeof(derived)); return 0; } gcc I believe prints out 2 , and Visual C++ 2010 prints out 1 . I

Is the default mode of ofstream implementation defined?

北城以北 提交于 2019-12-01 11:37:28
问题 Given the following code: std::ofstream stream("somefile"); if (!stream) { return 1; } When invoking .write(....) and using stdc++ and libc++ the stream is in binary mode ( std::ios::binary ). However when using MSVC (2015/2017RC1) it seems to be in text mode or something weird, because the the resulting file is larger than what is actually written. But if i explicitly set the mode std::ios::binary MSVC behaves similarly to the std::ofstream implementations of other standard libraries

Move semantics and function order evaluation

故事扮演 提交于 2019-11-29 22:05:43
Suppose I have the following: #include <memory> struct A { int x; }; class B { B(int x, std::unique_ptr<A> a); }; class C : public B { C(std::unique_ptr<A> a) : B(a->x, std::move(a)) {} }; If I understand the C++ rules about "unspecified order of function parameters" correctly, this code is unsafe. If the second argument to B 's constructor is constructed first using the move constructor, then a now contains a nullptr and the expression a->x will trigger undefined behavior (likely segfault). If the first argument is constructed first, then everything will work as intended. If this were a

Move semantics and function order evaluation

∥☆過路亽.° 提交于 2019-11-28 18:20:57
问题 Suppose I have the following: #include <memory> struct A { int x; }; class B { B(int x, std::unique_ptr<A> a); }; class C : public B { C(std::unique_ptr<A> a) : B(a->x, std::move(a)) {} }; If I understand the C++ rules about "unspecified order of function parameters" correctly, this code is unsafe. If the second argument to B 's constructor is constructed first using the move constructor, then a now contains a nullptr and the expression a->x will trigger undefined behavior (likely segfault).

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

拜拜、爱过 提交于 2019-11-28 08:58:04
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 <locale> . The explicit cast is introduced to select the desired overload.) Short answer No.

Is pointer comparison undefined or unspecified behavior in C++?

做~自己de王妃 提交于 2019-11-28 00:06:55
The C++ Programming Language 3rd edition by Stroustrup says that, Subtraction of pointers is defined only when both pointers point to elements of the same array (although the language has no fast way of ensuring that is the case). When subtracting one pointer from another, the result is the number of array elements between the two pointers (an integer). One can add an integer to a pointer or subtract an integer from a pointer; in both cases, the result is a pointer value. If that value does not point to an element of the same array as the original pointer or one beyond, the result of using

Is indexing a new map element and having something that reads it assigned to it undefined behaviour, or just unspecified?

心已入冬 提交于 2019-11-27 19:25:50
问题 After answering this question, there was a long discussion over whether the code in question was undefined behaviour or not. Here's the code: std::map<string, size_t> word_count; word_count["a"] = word_count.count("a") == 0 ? 1 : 2; First of all, it was well-established that this was at least unspecified. The result differs based on which side of the assignment is evaluated first. In my answer, I followed through each of the four resulting cases, with factors of which side is evaluated first

Why does a main function without a return statement return value 12?

眉间皱痕 提交于 2019-11-27 14:03:08
I have written a program that prints a table. I have not included the return syntax in the main function, but still whenever I type echo $? it displays 12. My source code : #include <stdio.h> int main(void) { int ans,i,n; printf("enter the no. : "); scanf("%d",&n); for(i=1;i<=10;i++) { ans = n*i; printf("%d * %d = %d\n",n,i,ans); } } I have not written return 12, but still it returns 12 every time I execute the program. Thanks. As swegi says, it's undefined behavior. As Steve Jessop et al say, it's an unspecified value until C89, and specified in C99 (the observed behavior is non-conformant to

Unspecified, undefined and implementation defined behavior WIKI for C

删除回忆录丶 提交于 2019-11-27 04:17:34
Although there is plentiful of links about this subject on SO, I think that there is something missing: a clear explanation in plain language of what are the differences between unspecified behavior (UsB), undefined behaviour (UB) and implementation-defined behavior (IDB) with detailed but easy explanation of any use-case and example. Note: I made the UsB acronym up for sake of compactness in this WIKI, but don't expect to see it used elsewhere. I know this may seem a duplicate of other posts (the one it comes closer is this ), but before anyone marks this as a duplicate , please consider what

What are the common undefined/unspecified behavior for C that you run into? [closed]

人盡茶涼 提交于 2019-11-27 04:09:55
问题 Closed . This question needs to be more focused. It is not currently accepting answers. Want to improve this question? Update the question so it focuses on one problem only by editing this post. Closed 6 years ago . An example of unspecified behavior in the C language is the order of evaluation of arguments to a function. It might be left to right or right to left, you just don't know. This would affect how foo(c++, c) or foo(++c, c) gets evaluated. What other unspecified behavior is there