473,385 Members | 1,342 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

why is it comming

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.

Nov 15 '05 #1
8 1313
-----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++) 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]=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-----
Nov 15 '05 #2
sahu <sw********@gmail.com> 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.
Nov 15 '05 #3
"sahu" <sw********@gmail.com> 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 (ir*******@freenet.de)
welcome to clc : http://www.ungerhu.com/jxh/clc.welcome.txt
clc faq-list : http://www.faqs.org/faqs/C-faq/faq/
clc frequent answers: http://benpfaff.org/writings/clc.
Nov 15 '05 #4
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.

Nov 15 '05 #5
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

Nov 15 '05 #6
"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.
Nov 15 '05 #7
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 <ma*****@earthlink.net> 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" <bh********@gmail.com> 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 (ir*******@freenet.de)
welcome to clc : http://www.ungerhu.com/jxh/clc.welcome.txt
clc faq-list : http://www.faqs.org/faqs/C-faq/faq/
clc frequent answers: http://benpfaff.org/writings/clc.
Nov 15 '05 #8

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.

Nov 15 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Miguel Manso | last post by:
Hi, list. I'm into a psicological doubt that I would like to share with you (you'll know why later on this mail). I'm a programmer with 5 year of experience into Perl. I'm on that point where...
1
by: Supra | last post by:
' 2004 'An unhandled exception of type 'System.NullReferenceException' occurred in system.windows.forms.dll 'Additional information: Object reference not set to an instance of an object. Option...
2
by: Supra | last post by:
An unhandled exception of type 'System.NullReferenceException' occurred in system.windows.forms.dll Additional information: Object reference not set to an instance of an object. i don't know how...
1
by: ucasesoftware | last post by:
i need to get a value comming from another Windows Application. How can i do that ? it's a phone number to open the customer form by phone number thx for help
3
by: Edwin Knoppert | last post by:
I'm doing some c# in VWD. In VB-mode i can select a page event and VWD inserts the call for me. Why isn't this in c# ? What's the best way to do this?
1
by: madhuxml82 | last post by:
Dear Forum Members, I have generated an XML Schema and a Table of XMLType referencing the XML Schema. Now When I am Inserting the Data into the Table. I am getting the Error 0RA-30937: Error is...
0
by: Andrew Meador | last post by:
I have implemented a printing scenario where an html file is printed using the the following code: public void PrintHtmlFile(string url) { RegistryKey IERegKey; string header = null; string...
3
by: sumanta123 | last post by:
Hi All, I am using this command exp dev/dev@uatdb file='C:\database\dbbackup.dmp' for export the data from my database.It is successfully export into the dbbackup.dmp file. While import...
2
by: ghjk | last post by:
I have a php search page. When user click search button data goes to ajax page and validate.After that validated data pass to the same php page to qurey the db. These are my ajax and search code. ...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.