All of the examples I've been able to find online about incrementing a pointer causing a segfault involved the dereference of the pointer - what if I just want to increment it (for example at the end of a for loop) and I don't care if it ends up in invalid memory because I won't use it again. For example, in this program I just need to step by 4 every iteration, but I never dereference these pointers again after the last iteration.
float* leftRowPointer, resultRowPointer;
// assume they were correctly initialized
for (unsigned int i = 0; i < 4; ++i, leftRowPointer += 4, resultRowPointer += 4) {
// do some stuff
}
Do I need to do something like this instead?
for (unsigned int i = 0; i < 4; ++i) {
// same stuff
if (i != 3) {
leftRowPointer += 4;
resultRowPointer += 4;
}
}
Is there a better way to accomplish what I'm trying to do?
When I've tried it myself nothing bad seems to happen, but that's hardly a guarantee that it will always work, and unfortunately I don't have access to Valgrind or similar at work.
We're using the C++11 standard, fwiw, and I couldn't find anything in there that directly applies to this, but I'll be the first to admit that I don't know the standard well enough to have a good idea of where to look for it.