Velocity Reviews > why is it comming

# why is it comming

sahu
Guest
Posts: n/a

 09-09-2005
hello everyone here's some doubt for me as it came in my test n i i got
it wrong by the lecturer.
we all know in static allocation first of all we have to specify the
declaration of spaces in arrays then how did the foll. program print 0
1 2 3 4 5 instead of 0 1 2 3 4.
the logic routine is like this:

int a[5],i;
for(i=0;i<=5;i++)
{a[i]=i;
}

so can anybdy tell why is it. the compiler used was tc.whether it
is takin the junk value to be printed as 5 or what pls do answer.
thanks to everyone in the grp.

Lew Pitcher
Guest
Posts: n/a

 09-09-2005
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

sahu wrote:
> hello everyone here's some doubt for me as it came in my test n i i got
> it wrong by the lecturer.
> we all know in static allocation first of all we have to specify the
> declaration of spaces in arrays then how did the foll. program print 0
> 1 2 3 4 5 instead of 0 1 2 3 4.
> the logic routine is like this:
>
> int a[5],i;
> for(i=0;i<=5;i++)

set i to 0
while i is less than *or equal to* 5,
do the following compound statement
and then add 1 to i

So, you are doing 6 iterations of the loop; one for each of
i == 0,
i == 1,
i == 2,
i == 3,
i == 4, and
i == 5

You really wanted only 5 iterations of the loop, so your termination
condition should have been
i < 5
to account for
i == 0,
i == 1,
i == 2,
i == 3, and
i == 4

> {a[i]=i;
> }
>
> so can anybdy tell why is it. the compiler used was tc.whether it
> is takin the junk value to be printed as 5 or what pls do answer.
> thanks to everyone in the grp.
>

- --

Lew Pitcher, IT Specialist, Enterprise Data Systems
Enterprise Technology Solutions, TD Bank Financial Group

(Opinions expressed here are my own, not my employer's)
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (MingW32)

iD8DBQFDIcM0agVFX4UWr64RAr8IAKDUP8tb84rwgmZh58yktb cRgV2YBACfasPw
H23Uf+Pq13BYRMQRjoxo/dw=
=fD5F
-----END PGP SIGNATURE-----

Christopher Benson-Manica
Guest
Posts: n/a

 09-09-2005
sahu <(E-Mail Removed)> wrote:

> we all know in static allocation first of all we have to specify the
> declaration of spaces in arrays then how did the foll. program print 0
> 1 2 3 4 5 instead of 0 1 2 3 4.

What you supplied isn't a program, nor does it contain any output
code. But there is still a problem in the code you did provide:

> int a[5],i;
> for(i=0;i<=5;i++)
> {a[i]=i;
> }

The array a has five elements, numbered 0 through 4. Your loop,
however, executes six times, and attempts to assign a value to a
non-existant element of a.

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.

Irrwahn Grausewitz
Guest
Posts: n/a

 09-09-2005
"sahu" <(E-Mail Removed)> wrote:
>hello everyone here's some doubt for me as it came in my test n i i got
>it wrong by the lecturer.
> we all know in static allocation first of all we have to specify the
>declaration of spaces in arrays then how did the foll. program print 0
>1 2 3 4 5 instead of 0 1 2 3 4.
> the logic routine is like this:
>
> int a[5],i;
> for(i=0;i<=5;i++)
> {a[i]=i;

Undefined behaviour the last time the loop is iterated: in the case
of i == 5 the expression a[5] = 5 writes to memory you don't own,
absolutely anything can happen from this point on.

Best regards.
--
Irrwahn Grausewitz ((E-Mail Removed))
welcome to clc : http://www.ungerhu.com/jxh/clc.welcome.txt
clc faq-list : http://www.faqs.org/faqs/C-faq/faq/

Martin Ambuhl
Guest
Posts: n/a

 09-09-2005
sahu wrote:
> hello everyone here's some doubt for me as it came in my test n i i got
> it wrong by the lecturer.
> we all know in static allocation first of all we have to specify the
> declaration of spaces in arrays then how did the foll. program print 0
> 1 2 3 4 5 instead of 0 1 2 3 4.

There is no way to address the question of output, since your snippet
contains no output function calls. However,

> the logic routine is like this:
>
> int a[5],i;
> for(i=0;i<=5;i++)

^^^^
this is certainly wrong and, when you try to assign 5 to the
non-existant a[5], you overflow the end of the array and your program
ceases to have defined behavior.

> {a[i]=i;
> }
>
> so can anybdy tell why is it. the compiler used was tc.whether it
> is takin the junk value to be printed as 5 or what pls do answer.
> thanks to everyone in the grp.
>

XXXXXX.working.in.my.blood
Guest
Posts: n/a

 09-10-2005
hello
i tried the program snippet, with TC
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.

regards
bharath

Keith Thompson
Guest
Posts: n/a

 09-10-2005
"XXXXXX.working.in.my.blood" <(E-Mail Removed)> 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

> 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

#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) http://www.velocityreviews.com/forums/(E-Mail Removed) <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.

Irrwahn Grausewitz
Guest
Posts: n/a

 09-10-2005

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

[Context restored]

>sahu wrote:

[...]
>> int a[5],i;
>> for(i=0;i<=5;i++)

Martin Ambuhl <(E-Mail Removed)> wrote:
>this is certainly wrong and, when you try to assign 5 to the
>non-existant a[5], you overflow the end of the array and your program
>ceases to have defined behavior.

"XXXXXX.working.in.my.blood" <(E-Mail Removed)> wrote:
>hello
>i tried the program snippet, with TC
>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.

Not at all. Once you invoke undefined behaviour absolutely anything
may happen. Unfortunately, this includes _seemingly_correct_
operation of the program. It was just bad luck that the program
produced any output at all.

Best regards
--
Irrwahn Grausewitz ((E-Mail Removed))
welcome to clc : http://www.ungerhu.com/jxh/clc.welcome.txt
clc faq-list : http://www.faqs.org/faqs/C-faq/faq/

sahu
Guest
Posts: n/a

 09-10-2005

thanks all
at least now iam satisfied that i was correct in my test n i lost my
marks just for nothing.thnk u once again to the grp.