unspecified-behavior Questions

5

Solved

C++ primer, 5th, 14.8.2, Using a Library Function Object with the Algorithms: vector<string *> nameTable; // vector of pointers // error: the pointers in nameTable are unrelated, so < is u...
Forearm asked 13/3, 2017 at 11:33

4

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, st...

1

I was having a discussion regarding using variables with indeterminate values leading to unspecified behavior, rather than undefined behavior, as discussed here. This assuming that a variable with ...
Benoite asked 22/2, 2023 at 13:53

2

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<int> is unspecified. But ...

1

In the Linux kernel, I found the following code: static inline loff_t pos_from_hilo(unsigned long high, unsigned long low) { #define HALF_LONG_BITS (BITS_PER_LONG / 2) return (((loff_t)high <&l...
Dilatory asked 6/8, 2021 at 15:30

9

Solved

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

2

Solved

I understand that when I call a function such as a(b(),c()); then the behavior of this may be undefined in <= C++14, and unspecified in >= C++17, in the sense that it is up to the comp...
Fastigium asked 24/5, 2019 at 9:40

1

Solved

Consider the following code: #include <cctype> #include <functional> #include <iostream> int main() { std::invoke(std::boolalpha, std::cout); // #1 using ctype_func = int(*)(...
Boozy asked 15/4, 2019 at 10:20

3

The following code works in clang++, but crashes spectacularly in g++ #include<vector> #include<iostream> template<class Iterator> double abs_sum(double current_sum, Iterator it...
Across asked 14/12, 2018 at 10:38

4

Solved

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 resul...

2

C++ standard If a C++14 implementation includes padding bits in the underlying bytes of an unsigned int , does the standard specify if bitwise operations must not be performed on padding bits ? A...

2

Solved

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 he...

1

Solved

According to cppreference (emphasis mine): A core constant expression is any expression that does not have any one of the following in any subexpression (...) An expression whose evaluat...

2

Solved

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). ...
Jerkwater asked 23/2, 2017 at 10:15

2

Solved

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 ...

1

Solved

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 fa...
Weslee asked 2/8, 2015 at 17:37

2

Solved

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:...
Jutland asked 8/4, 2015 at 11:40

5

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...

1

There is a claim in Which headers in the C++ standard library are guaranteed to include another header?: The C++ standard library headers may include each other in unspecified ways, so programm...
Stoltz asked 26/2, 2015 at 16:20

2

Solved

Suppose following piece of code: #include <iostream> using namespace std; char one() { cout << "one\n"; return '1'; } char two() { cout << "two\n"; return '2'; } int main(...
Markel asked 23/8, 2014 at 20:52

11

Solved

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 ...

2

Solved

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[...

2

Solved

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 ...
Laminated asked 1/3, 2013 at 21:8

3

Solved

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...

4

Solved

The comma sequence operator introduces a sequence point in an expression. I am wondering whether this means that the program below avoids undefined behavior. int x, y; int main() { return (x++, ...
Marilee asked 18/12, 2012 at 15:12

© 2022 - 2024 — McMap. All rights reserved.