4 ms·
The other day I thought "I'm finally gonna dive deep into C++, and I'm going to start out right, with a mindset toward modern C++ and statically forbidding memo
by 2bitencryption 10y ago
The other day I thought "I'm finally gonna dive deep into C++, and I'm going to start out right, with a mindset toward modern C++ and statically forbidding memory errors, etc."
First thing I did was write up a test program that looks something like this, to see what errors I would get:
int x = 42;
int* px = &x;
px++;
*px = 12;
Compiled on GCC with -Wall. No warnings. I thought for sure some warning must prevent pointer arithmetic. After asking around I guess what I did above wasn't really pointer arithmetic, at least not in the compiler's eyes, so it's not warned against.
- mfukar 10y agoSo you are disappointed some random language isn't more like what you imagined. Great. Here's to hoping you don't go about your day with the same attitude.
- 2bitencryption 10y agoheh not really. more like "I'm disappointed that gcc/clang doesn't even give me the option to warn me from doing something that I will surely never want to do, even if it is "legal" (but undefined) C++, for the sake of safety."
- voyou 10y agoPointer arithmetic isn't "something that I will surely never want to do"; it's something you want to do all the time in C (and therefore in C++ written in a more C-like style). Illegitimate uses of pointer arithmetic, such as your example, are only easy to detect in trivial cases like yours. I assume this is why no one has bothered implementing a warning for this special case. In C++, directly using pointers to access arrays is often discouraged, in favour of using the standard collection types and their associated iterator types. So in code that follows this style, it is possible to warn about uses of pointer arithmetic, and the core C++ guidelines do suggest such a warning. But the guidelines also point out that "this rule would generate a huge number of false positives if applied to an older code base": https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#r2-in-interfaces-use-raw-pointers-to-denote-individual-objects-only https://github.com/isocpp/CppCoreGuidelines/blob/master/CppC...
- 2bitencryption 10y agoYes! So that's why I'm so disappointed I couldn't find a warning discouraging all use of pointer arithmetic.
- Flow 10y agoThere are plenty more warnings to turn on than what -Wall uses. http://stackoverflow.com/questions/11714827/how-to-turn-on-literally-all-of-gccs-warnings http://stackoverflow.com/questions/11714827/how-to-turn-on-l...
- 2bitencryption 10y agounfortunately it passes -Weverything on clang, as well.