Talk:Undefined behavior
UntitledI got rid of the todo-box comment that "UB is not a feature", because clearly it is a feature of many programming languages. (It's not usually a "feature" in the marketingspeak sense, of course.) I also subst'ed the {{todo}} template, so it would be around to comment on. I don't mind if it's removed. (The anonymous editor makes a good point about the #pragma paragraph, though; it really is out of place.) --Quuxplusone 02:47, 6 December 2006 (UTC)
Sorry, I saw after commenting that you were logged in; I tend to assume that if a Talk-page comment isn't signed, it's "anonymous". :) I subst'ed the template so that it would be here on the page in case anyone wondered in three months what I was talking about... except that I just noticed(!) that it didn't actually substitute in the text, which is what I was trying to do. So I've reverted my subst'ing. Regarding "feature": UB is a "feature" of C and C++ in the same way that currying is a "feature" of ML, or funny operators are a "feature" of APL: it's something a C or C++ programmer is going to have to deal with, because it's a feature of the language. If there's a better word, I'd support it; the only other word I can think of right now is "element", and that has even more confusing overloads in a programming context. :) --Quuxplusone 23:37, 6 December 2006 (UTC) I took out the nasal demons part of the todo-box because it is just "[r]ecognized shorthand on the Usenet group comp.std.c for any unexpected behavior of a C compiler on encountering an undefined construct.", not actual demons coming out of your nose. In other words, unexpected results is a perfectly possible result of undefined behavior. Also, maybe the todo-box should be taken out altogether because the article currently mentions that undefined and implementation-defined behavior are different. — Daniel 00:19, 13 July 2007 (UTC)
Emacs first, or NetHack first?
So, the only "verifiable" paper source we have — Unix Review — gives the wrong information! --Quuxplusone (talk) 21:47, 2 November 2008 (UTC) Unspecified behaviorIt be great if this article explained the differences of undefined behavior with unspecified behavior. --Abdull (talk) 09:05, 4 December 2009 (UTC) OptimizationsShould any examples be given for compilers exploiting undefined behavior for optimization purposes? For example GCC optimizes away a lot of comparisons involving signed integers by ignoring the possibility of overflow. Hiiiiiiiiiiiiiiiiiiiii (talk) 20:18, 18 May 2011 (UTC)
#pragma jokeI think the #pragma joke section does not belong here. #pragma is not undefined, it is implementation-defined behavior. And even though we do not have the specific article, I believe unspecified behavior is a much better match. --Mormegil (talk) 21:23, 21 October 2012 (UTC)
Undefined behaviour resulting from pointer arithmeticTo User:Namezero111111 and other editors who are changing the example which uses pointer arithmetic (via array subscripting) to produce undefined behaviour, please refer to ISO/IEC 9899:1999 §6.5.6 ¶8: "When an expression that has integer type is added or subtracted from a pointer, the result has the type of the pointer operand… If both the pointer and result point to elements of the same array object, or one past the last element of the array object, the evaluation shall not produce an overflow; otherwise, the behavior is undefined." As you can see, the mere evaluation of such an expression results in undefined behaviour; no dereferencing or assignment need have occurred. —Psychonaut (talk) 09:59, 9 September 2013 (UTC) getsThere's an entire function in C that has undefined behaviour: gets. Nothing guarantees that the string from stdin fits into the buffer passed to the function. --88.113.189.17 (talk) 15:47, 1 November 2013 (UTC)
Why is the integer overflow thing listed as a benefit?There is no advantage at all to this example. All it means is that the "naïve" method of detecting overflow cannot work due to being UB, therefore developers have to bend over backwards to somehow detect the overflow before it happens. It's easy to understand why it's UB when one remembers the C standard does not in any way guarantee that a platform uses Two's complement and assuming it does is bad, but I don't see why one would spin that as a good thing. Medinoc (talk) 14:06, 2 September 2017 (UTC)
Outdated example: i = i++ + 1 is no longer undefined behavior in C++17.The article states that The following example will however cause undefined behavior in both C++ and C. i = i++ + 1; // undefined behavior
This is not true in C++17, because = has been added as a sequence point, forcing the right side to evaluate before the left. See https://stackoverflow.com/q/47702220 for details and sources. A better example might be i++ + i++. — Preceding unsigned comment added by 2003:D8:DBC9:8700:9D1E:F4B2:5AC6:B463 (talk) 06:57, 9 April 2019 (UTC)
Undefined behaviour caused by dereferencing a null pointerThe below example is included in the text but if this is C code, this is incorrect as the NULL pointer is implementation defined. This means that it is not necessarily zero. int arr[4] = {0, 1, 2, 3}; int *p = arr + 5; // undefined behavior for indexing out of bounds p = 0; int a = *p; // undefined behavior for dereferencing a null pointer
In order to make clear the distinction I would recommend replacing:
with
for C or:
for C++. — Preceding unsigned comment added by MaxCampman (talk • contribs) 19:17, 10 April 2019 (UTC)
Using uninitialized objects of automatic storage durationThe article asserts that "In C the use of any automatic variable before it has been initialized yields undefined behavior". However, this does not seem to be the case. See this[1] post by Jens Gustedt, for example, as well as various[2] WG14 discussions. Worse yet, this appears to be rather underspecified[3], and different to how it is treated by C++[4][5]. I'm struggling to think of the best way to update the article to reflect this information, without unnecessarily confusing people. Deltax64 (talk) 06:14, 11 March 2021 (UTC) References
|