Questions tagged [unspecified-behavior]

Unpredictable, but valid, behavior of a program that an implementation is not required to document.

67 questions
647
votes
9 answers

Undefined, unspecified and implementation-defined behavior

What is undefined behavior (UB) in C and C++? What about unspecified behavior and implementation-defined behavior? What is the difference between them?
96
votes
2 answers

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

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" ; …
72
votes
11 answers

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

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…
Benoit
  • 37,894
  • 24
  • 81
  • 116
54
votes
3 answers

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

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…
Pascal Cuoq
  • 79,187
  • 7
  • 161
  • 281
46
votes
1 answer

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

Consider the following code: #include #include #include int main() { std::invoke(std::boolalpha, std::cout); // #1 using ctype_func = int(*)(int); char c =…
L. F.
  • 19,445
  • 8
  • 48
  • 82
39
votes
4 answers

Is "-1>>5;" unspecified behavior in C?

C11 §6.5.7 Paragraph 5: The result of E1 >> E2 is E1 right-shifted E2 bit positions. If E1 has an unsigned type or if E1 has a signed type and a nonnegative value, the value of the result is the integral part of the quotient of E1 / 2*^E2. If…
msc
  • 33,420
  • 29
  • 119
  • 214
38
votes
5 answers

How to implement memmove in standard C without an intermediate copy?

From the man page on my system: void *memmove(void *dst, const void *src, size_t len); DESCRIPTION The memmove() function copies len bytes from string src to string dst. The two strings may overlap; the copy is always done in a…
Pascal Cuoq
  • 79,187
  • 7
  • 161
  • 281
27
votes
3 answers

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

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…
ctor
  • 5,938
  • 2
  • 26
  • 37
20
votes
2 answers

Sequence Point ambiguity, undefined behavior?

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 using namespace std; int len_…
18
votes
2 answers

Is there a reason why implementations allow instantiation of std::complex with unsupported types

edit note: originally question said illegal where now it says unspecified. Thanks to video comment section of Jason Turner video recently I learned that std::complex is unspecified. But all(AFAIK) implementations seem to happily…
NoSenseEtAl
  • 28,205
  • 28
  • 128
  • 277
18
votes
3 answers

Is it unspecified behavior to compare pointers to different arrays for equality?

The equality operators have the semantic restrictions of relational operators on pointers: The == (equal to) and the != (not equal to) operators have the same semantic restrictions, conversions, and result type as the relational operators except…
Fred Nurk
  • 13,952
  • 4
  • 37
  • 63
18
votes
5 answers

Strange values while initializing array using designated initializers

When I initialize the array below all the output looks ok except for values[3]. For some reason values[3] initialized as values[0]+values[5] is outputting a very large number. My guess is that I am trying to assign values[0]+values[5] before they…
16
votes
4 answers

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

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 int main(void) { int ans,i,n; printf("enter the…
zarna
  • 263
  • 2
  • 5
15
votes
2 answers

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

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…
Ben Jackson
  • 90,079
  • 9
  • 98
  • 150
1
2 3 4 5