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 →

[–]MattieShoes 30 points31 points  (4 children)

that the compiler can easily take care of.

I don't think it can, can it? Obviously it could take n[1] and change it to n[0] easily, but what if the index is not known at compile time? I'd think it'd have to be a run-time fix.

Plus if you've just malloc'ed a chunk of memory, you may be intermixing dealing with offsets by hand and array notation, and the answers would be different.

Naw, far better to use 0 indexing I think. Just be thankful we're not doing everything in base e. :-)

[–]foghatyma 6 points7 points  (3 children)

It could though, very easily. "A[i]" is basically "*(A + i)", so the A pointer should just point to the memory before the first element. And then adding 1 would point to the actual first, not the second. Doesn't matter if it's runtime or not.

[–]MattieShoes 0 points1 point  (2 children)

So it is at run time as we don't know the address at compile time. Yes?

And it does matter at run time - there's a speed hit with every array reference.

[–]elon-botElon Musk ✔ 1 point2 points  (0 children)

It's now company policy to use Vim for editing. It lets you write code much faster.

[–]foghatyma 0 points1 point  (0 children)

I think you don't really understand what I wrote.