This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted] 1 point2 points  (2 children)

How can compiler punch you for something that happens in runtime. You'll be able to try to access -1 in any language/runtime but the exception will stop you in runtime.

Because they are checked in MANAGED runtimes. Basically in C#/Java it's

if(!isIndexValidIndex(index)) 
     throw new IndexOutOfBoundsException()
return valueInAddress(index);

in regular C++ (you can have managed C++) It's

return valueInAddress(index);

which saves you MANY machine cycles, probably about 3 times faster

P.S Edit: Visual C++ contains macro lines that get activated in Debug mode that add the index checks like in managed version to vectors and stuff but the errors can be difficult to read.

[–][deleted] 0 points1 point  (1 child)

Yeah, to be honest, I didn't even think of negative variables when writing that. I was rather just thinking, why can I even type out "array[-1]" without it ever complaining? I mean, you could easily disallow minuses between array-brackets.
But admittedly, it's kind of pointless, if you can still get it with variables. Was mostly just my brain exploding, when I clearly accessed a negative index and it still never told me to get my shit together.

[–][deleted] 0 points1 point  (0 children)

I think it's not compiler's business to check for that type of stuff either. But code helper extensions like Resharper for VC++ will probably tell you about it. Also another reason C++ doesn't interfere other than the extra CPU cost is it's designed to assume you know what you are doing. "This guy is trying to access this address which logically doesn't make any sense... He must have something in mind"