"XXXXXX.working.in.my.blood" <bh********@gmail.com> writes:
i tried the program snippet, with TC
You tried what program snippet? For the Nth time, don't assume that
readers can easily see the article to which you're replying. You need
to provide some context, so each article can be read on its own.
If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers.
i tried to print both i and a[i].
guess what i got the same output for each of them
it was like
001122334455
the first one is the i and the second one is a[i]
so even if the array is supposed to have 5 elements, i got the output
like this
isn't it strange.
Ok, here's a program that demonstrates what I *think* you're talking
about
:
#include <stdio.h>
int main(void)
{
int a[5];
int i;
for (i = 0; i <= 5; i ++) {
a[i] = i * 10;
}
for (i = 0; i <= 5; i ++) {
printf("i = %d, a[i] = %d\n", i, a[i]);
}
return 0;
}
When I run this, I get the following output:
i = 0, a[i] = 0
i = 1, a[i] = 10
i = 2, a[i] = 20
i = 3, a[i] = 30
i = 4, a[i] = 40
i = 5, a[i] = 50
It looks like I'm accessing 6 elements of a 5-element array, even
though a[5] isn't really part of the array.
Accessing memory beyond the end of the array invokes undefined
behavior. In this case, what it *probably* does is access a chunk of
memory just past the end of the array. It could step on another
variable, it could step on something critical that the compiler
depends on, or it could just be harmlessly using a piece of memory
that isn't being used for anything else.
It's up to you to avoid undefined behavior. You can't expect the
compiler to catch it for you.
--
Keith Thompson (The_Other_Keith)
ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.