undefined-behavior

Any C compiler where “==” evaluates to larger than one?

风流意气都作罢 提交于 2019-12-23 07:07:52
问题 As anything non-zero means true, but the > , < , == etc. operators returning 1 for true, I'm curious if there are any notable C compilers where these operators can result in a value greater than 1 . In other words, is there any compiler where int i = (a==b) ; would result in undefined behavior if I intended to use i not as a boolean value, but as an integer, and was assuming it would be either 0 or 1 ? 回答1: No, if there are, they're not C compilers :-) The standard mandates that relational

Valgrind shows memory leak in std::make_unique

我的未来我决定 提交于 2019-12-23 06:48:10
问题 I'm using Valgrind to check for memory leaks. Unfortunately I get a Leak_DefinitelyLost warning. Attached is a simplified version of my code that reproduces the error: #include <iostream> #include <vector> #include <memory> #include <unordered_map> using namespace std; class Base{ public: explicit Base(double a){ a_ = a; } virtual void fun() = 0; protected: double a_; }; class Derived_A : public Base{ public: Derived_A(double a, vector<double> b, vector<double> c): Base(a), b_{b}, c_{c}{ }

Is casting of infinity to integer undefined?

冷暖自知 提交于 2019-12-23 06:47:21
问题 Is the casting of infinity (represented by float) to an integer an undefined behavior? The standard says: 4.10 Floating-integral conversions A prvalue of a floating point type can be converted to a prvalue of an integer type. The conversion truncates; that is, the fractional part is discarded. The behavior is undefined if the truncated value cannot be represented in the destination type. but I can't tell whether "truncated value cannot be represented" covers infinity. I'm trying to understand

Is a C++ compiler allowed to emit random code once it encounters a construct that is classified as undefined behavior? [duplicate]

坚强是说给别人听的谎言 提交于 2019-12-23 06:23:11
问题 This question already has answers here : Closed 9 years ago . Possible Duplicate: Undefined, unspecified and implementation-defined behavior I'm trying to deepen my understanding of undefined behavior in C++. Suppose a C++ compiler will intentionally detect some cases of undefined behavior - for example, modifying the variable twice between two sequence points: x++ = 2; Once that imaginary compiler reliably detects such a situation it will say emit ten totally random machine instructions into

C operator += Sequence point?

喜欢而已 提交于 2019-12-23 05:33:09
问题 Is this defined behaviour? *p += *p--; And, if it is, is it equivalent to { p[0] += p[0]; --p; } or to { p[-1] = p[0]; --p; } ? I'm guessing the being defined or not depends on whether += has an implicit sequence point and, if it has, my guess is that the second block should be the correct one. EDIT: I think it's not a duplicate of the suggested question because the main question there is what are sequence points and how do the affect behaviour. In my case I have clear idea of what a sequence

C static inline function redefinition rules

穿精又带淫゛_ 提交于 2019-12-22 22:27:41
问题 Suppose in bar.h there might exist: static inline int fun () { return 2; } And to ensure that fun is always defined foo.h contains the following: static inline int fun () { return 3; } Does the following elicit undefined behavior when bar.h contains the definition? #include "foo.h" /* ensure there is always a definition */ #include "bar.h" /* use the bar definition if it exists */ int main () { /* ... */ int x = fun (); /* ... */ With gcc (4.0.1) (old, but it's what I have currently) the

C static inline function redefinition rules

元气小坏坏 提交于 2019-12-22 22:25:47
问题 Suppose in bar.h there might exist: static inline int fun () { return 2; } And to ensure that fun is always defined foo.h contains the following: static inline int fun () { return 3; } Does the following elicit undefined behavior when bar.h contains the definition? #include "foo.h" /* ensure there is always a definition */ #include "bar.h" /* use the bar definition if it exists */ int main () { /* ... */ int x = fun (); /* ... */ With gcc (4.0.1) (old, but it's what I have currently) the

Undefined behavior and sequence point

丶灬走出姿态 提交于 2019-12-22 08:24:45
问题 From past few days I was trying to learn about undefined behavior. Few days ago I found a c-faq link. This helps a lot to clear many confusions, but creates an another big confusion when I read the question #3.8. After my lots of efforts to understand the statement (specially second sentence); The Standard states that Between the previous and next sequence point an object shall have its stored value modified at most once by the evaluation of an expression. Furthermore, the prior value shall

Computing the address of an object from the address of one of its member subobject

为君一笑 提交于 2019-12-22 05:16:49
问题 I am in the following situation: //This is Public class B{/*usefull stuff*/}; B*f(); void g(B*b)(); //Those classes are only declared the translation unit of f and g. class Whatever1{/*Implementation details only useful to f and g*/}; class Whatever2{/*Implementation details only useful to f and g*/}; class A{ public: Whatever1 w1; Whatever2 w2; B b; }; In function g, I want to convert the parameter (a pointer to B) to a pointer to A. B instances are always wrapped in A instances. I ended up

Does put_money hold its argument by value or reference?

不想你离开。 提交于 2019-12-22 05:07:54
问题 Does the following invoke undefined behavior? #include <iostream> #include <iomanip> #include <algorithm> #include <experimental/iterator> int main() { long double values[] = {1, 2, 3}; std::transform( std::begin(values), std::end(values), std::experimental::make_ostream_joiner(std::cout, ", "), [](long double v) { return std::put_money(v + 1); } ); return 0; } My worry is that return std::put_money(v + 1) returns a reference to the temporary v + 1 . 回答1: The standard ([ext.manip]/6) only