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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
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, ...
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 ...
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 ...
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 ...
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 ...
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: ...
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 ...
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 &...
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:", ...
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 ...
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 ...
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 ...
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 ...
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 ...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
c++ × 2769c × 291
design × 262
object-oriented × 236
design-patterns × 196
java × 153
c++11 × 133
coding-style × 99
object-oriented-design × 98
c# × 96
data-structures × 80
multithreading × 76
programming-practices × 73
algorithms × 71
performance × 71
programming-languages × 69
qt × 66
inheritance × 61
compiler × 61
unit-testing × 60
architecture × 58
libraries × 57
python × 56
interfaces × 54
memory × 53