Hacker News new | ask | show | jobs
by 3a2d29 1318 days ago
Wouldn’t the performance costs of bounds checking on arrays be the same if the computer was doing it or if your code was doing it?

By that logic C/C++ doing no bounds checking speeds your code up?

3 comments

> Wouldn’t the performance costs of bounds checking on arrays be the same if the computer was doing it or if your code was doing it?

It depends. The C programmer can choose to do the bounds checking in a for loop by just checking once before the loop begins, or once per iteration even if an array is accessed multiple times in the loop, or the safe language might have more overhead than a simple if statement in the C code. This can, of course, go the opposite direction (the safe language has verified the loop bounds, but the C programmer is checking before every array access). It's a battle between the C programmer and the designer and/or implementer of the safe language.

One of the reasons I like C is it gives you more control. This can be a good or a bad thing. This can lead to some really performant code you couldn't do in most languages or it can lead to some gnarly security problems. Maybe both in the same spot of code.

I use C to write mostly pet projects at home. I use it at work without having a choice in the matter.

Yes, which is why compiling on different optimization settings will have bounds checking on or off in C++
Well, yes, it does. Whether or not that’s a good tradeoff is a different question.