3 ms·
If dereferencing an invalid pointer reliably caused a segfault, this would be true. But it's undefined behaviour, so it can segfault, corrupt data, leak data, e
by twic 4y ago
If dereferencing an invalid pointer reliably caused a segfault, this would be true. But it's undefined behaviour, so it can segfault, corrupt data, leak data, etc. Given that the possible behaviour is a superset of what can go wrong using an integer index, i would say it's worse.
I would agree that using naive integer indices, and running the risk of accessing the wrong data, is also completely unacceptable, though.
- jstanley 4y agoI'm agreeing with you. I'm saying that a bounds checker is better than a segfault because it always works. (You're right that it actually doesn't always work because sometimes your stale index will still be inside the array but refer to a different thing, but at least it can't be abused to write into other arrays).