why is it comming

S

sahu

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;
}

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.
 
L

Lew Pitcher

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
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++)
Reread this line to yourself

It reads
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;
}

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)

iD8DBQFDIcM0agVFX4UWr64RAr8IAKDUP8tb84rwgmZh58yktbcRgV2YBACfasPw
H23Uf+Pq13BYRMQRjoxo/dw=
=fD5F
-----END PGP SIGNATURE-----
 
C

Christopher Benson-Manica

sahu said:
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;
}


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.
 
I

Irrwahn Grausewitz

sahu said:
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;


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.
 
M

Martin Ambuhl

sahu said:
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;
}

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.
 
X

XXXXXX.working.in.my.blood

hello
i tried the program snippet, with TC
i tried to print both i and a.
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

so even if the array is supposed to have 5 elements, i got the output
like this
isn't it strange.

regards
bharath
 
K

Keith Thompson

XXXXXX.working.in.my.blood said:
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.
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

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 * 10;
}

for (i = 0; i <= 5; i ++) {
printf("i = %d, a = %d\n", i, a);
}

return 0;
}

When I run this, I get the following output:

i = 0, a = 0
i = 1, a = 10
i = 2, a = 20
i = 3, a = 30
i = 4, a = 40
i = 5, a = 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.
 
I

Irrwahn Grausewitz

Please preserve some context and the attribution lines when you
reply to a message.

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.

[Context restored]
sahu wrote: [...]
int a[5],i;
for(i=0;i<=5;i++)

Martin Ambuhl said:
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 said:
hello
i tried the program snippet, with TC
i tried to print both i and a.
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

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
 
S

sahu

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top