Skip to main content
34 votes

Unstable output C++: running the same thing twice gives different output

Cherish this bug. Do nothing to harm it. There is no reason to suppose it is new. It could well be a bug which has been in the software all along, and which has now been revealed as a result of your ...
Martin Kochanski's user avatar
33 votes
Accepted

Unstable output C++: running the same thing twice gives different output

Sometimes the program works well for 20 runs at a time. So, it's gonna take forever to test whilst adding each change one by one and testing each new change... First, you need to work on this. Find a ...
Doc Brown's user avatar
  • 211k
21 votes

Immutability across programming languages

This is a common confusion with immutability. There's a difference between an immutable value and an immutable reference. For example, say you have x = 10. You can't do something like 10 = 20 to ...
Karl Bielefeldt's user avatar
16 votes

Unstable output C++: running the same thing twice gives different output

Update: when compiling/building the .exe and running, the wrong output does not seem to happen. That is a major clue. When you're debugging, you're almost certainly debugging a debug build. Debug ...
Andrew Henle's user avatar
8 votes

Unstable output C++: running the same thing twice gives different output

There's already a lot of good advice here in answers and comments, a final answer might be a meld of different things. Since you added that the error condition happens only when debugging and not when ...
mr3's user avatar
  • 189
6 votes

Immutability across programming languages

Welcome to one of the fun quirks of programming in different languages (and their different semantics) In a general sense, whether something has the property of mutability, or if there is simply ...
Michael Shaw's user avatar
  • 10.1k
5 votes

Is "using active record pattern" a reason to inherit from standard container (eg:vector)?

This has nothing to do with the ActiveRecord pattern, that's a red herring. What you are trying to do here is (more or less) to implement the equivalent of a C# extension method in C++. Specifically, ...
Doc Brown's user avatar
  • 211k
4 votes

Immutability across programming languages

In general, the mutability says that the values of object can be changed. Your C++ example is indeed mutability, because x is an int object, that the value 10 is stored in the object and that the ...
Christophe's user avatar
  • 79.9k
4 votes

Is "using active record pattern" a reason to inherit from standard container (eg:vector)?

No, the active record pattern is not at all a justification for inheriting of standard containers (at least in C++). But the fact that you mention it, suggests that separation of concerns could be ...
Christophe's user avatar
  • 79.9k
4 votes

Unstable output C++: running the same thing twice gives different output

One key advice for dealing with such problems has not been mentioned yet: Run the code using the valgrind memory checker if possible! The point is, the C famliy of languages, along with some other ...
cmaster - reinstate monica's user avatar
3 votes

"use auto" and "declare most abstract type", which guideline has higher priority?

I know I should declare most abstract type when possible I think this rule-of-thumb (it is actually nothing more than that!) is vastly overstated. Karl Bielefeld's answer to that first linked ...
Doc Brown's user avatar
  • 211k
3 votes

How to prevent 'global variables' in a big project?

In the new bright world of C++ Core Guidelines (endorsed by no less than Bjarne Himself and current WG21 convener Herb Sutter) - there is Clang-tidy, and its avoid-non-const-global-variables option: ...
No-Bugs Hare's user avatar
2 votes

OOP Design of a Mathematical Group

A group is a triple of a set, an associative binary operation, and the identity element of that binary operation, with the requirement that every element have an inverse. The element of one group is ...
Caleth's user avatar
  • 11.5k
2 votes

Immutability across programming languages

I'd start with C and C++, where things are reasonably simple. You have "objects" in memory, not in the sense of object oriented programming, but items that can be read or changed. These &...
gnasher729's user avatar
  • 46.7k
1 vote

Immutability across programming languages

x = 10 x = 20 This is not shadowing. This is shadowing: x = 0 def outer(): x = 1 def inner(): x = 2 print("inner:", x) inner() print("outer:", ...
candied_orange's user avatar
1 vote

Immutability across programming languages

The dangers of mutation specifically apply to alias mutation; a lot of times when people claim "immutability", what they really claim (or want) is alias immutability. An alias is a value ...
tarzh's user avatar
  • 119
1 vote

In C++, why does the main function use 'char *argv[]'?

The operating system has code that sets up these parameters and calls main() with these parameters. The version for C is easy to implement: The arguments are stored and we get one char* for each, then ...
gnasher729's user avatar
  • 46.7k
1 vote

Are there real world examples demonstrating reasonable performance improvement by using move semantics?

There are objects that can exist only once. For example, an open file with exclusive read/write access to a file on your hard drive. You can’t have two of these objects, even for the shortest amount ...
gnasher729's user avatar
  • 46.7k
1 vote

C/C++: Which conversion warnings make sense in practice?

Here's what decades of experience have taught me. Turn on all the conversion warnings and tell the compiler to treat all warnings as errors. The conversions that must happen should be made explicit ...
Adrian McCarthy's user avatar
1 vote

Are utility classes with nothing but static members an anti-pattern in C++?

Whenever we're speaking about a potential anti-pattern - it is important to understand: what is so bad about it? Which RL bad consequences will come out of it? And in this particular case of ...
No-Bugs Hare's user avatar

Only top scored, non community-wiki answers of a minimum length are eligible