Open utterances-bot opened 3 years ago
Oldies but goodies! Probably time for these to be re-stated for a new audience - especially for those who haven't been taught c++ properly (at school/university!).
re Integer Overflows. Also that mixing signed/unsigned arithmetic operations doesn't always give the expected result type (gives unsigned when signed might be expected). In addition - when using unsigned for array offsets and descend rather than ascend, be careful of the terminating condition. This is wrong:
#include <iostream>
int main()
{
for (size_t i = 5; i >= 0; --i)
std::cout << i << ' ';
}
and will cause an infinite loop - as i is unsigned and therefore is always >=0 ! Should be something like:
#include <iostream>
#include <vector>
int main()
{
std::vector<int> vs {1, 2, 3, 4, 5};
for (size_t i = vs.size(); i > 0; --i)
std::cout << vs[i - 1] << ' ';
}
Also that c-style strings need an extra byte for the terminating null char. And that indexes start at 0 with a maximum index value of numOfElems - 1
@2kaud: your vector example would be safer using
for (auto i = vs.rbegin(); i != vs.rend(); --i) std::cout << *i << ' ';
because the iterator library is your friend. That way, you have no chance of, say, accidentally writing vs[i] and trying to read an out-of-bounds location.
The PrtHolder/PtrHolder example (no big deal, the compiler should pick up the typo) is an example of the old Rule of Three, which said that, whenever you have an assignment operator, copy constructor, or destructor, you need all three. (I never called it a copy assignment operator back then.) In this case, the default move constructor and move assignment operator will be just fine, so we don't actually have to invoke the Rule of Five. It is of course handier to use '= delete;' rather than the old 'private:' tric
Re the vector. Yes. But I was trying to make the point re using unsigned vs signed. The first for loop is OK with signed, but not with unsigned. The second is OK with both. If you already have working loops using signed, just changing the type to unsigned doesn't mean that they will still work.
Yes, but I'm seeing an attempt to drag C++ programmers into the 21st Century, not to bring them up to modern standards, and it bothers me. PtrHolder should be std::unique_ptr, and if nothing else the Rule of Three/Five should be mentioned. Nothing was said about move constructors or move assignment operators Iterators are useful, and solve a lot of potential correctness issues.
This reminds me of my son's college textbook on C++, back in 2011 or so. It basically covered C with Classes, and hadn't caught up with the ARM yet.
Heck, if anyone needs these reminders, they almost certainly don't have much of a clue about exception safety or floating point fuzziness.
"Heck, if anyone needs these reminders, "
takes me back to C++98.....
C++ Software Security Sins: Basic Issues - C++ Stories
C++ Software Security Sins In the world of software development, we are up against new cybersecurity threats each day, and the risks and consequences of un-secure software are too significant to be unaware of. Let’s review some common security threats that might lurk in our C/C++ code. This article is an adapted version of the presentation given by Mary Kelly, supported by Embarcadero.
https://www.cppstories.com/2021/security-sins/