why is it comming

Discussion in 'C Programming' started by sahu, Sep 9, 2005.

  1. sahu

    sahu Guest

    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.
     
    sahu, Sep 9, 2005
    #1
    1. Advertising

  2. sahu

    Lew Pitcher Guest

    -----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;
    > }
    >
    > 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-----
     
    Lew Pitcher, Sep 9, 2005
    #2
    1. Advertising

  3. sahu <> 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;
    > }


    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.
     
    Christopher Benson-Manica, Sep 9, 2005
    #3
  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.
    > 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.
    --
    Irrwahn Grausewitz ()
    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.
     
    Irrwahn Grausewitz, Sep 9, 2005
    #4
  5. 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;
    > }
    >
    > 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.
    >
     
    Martin Ambuhl, Sep 9, 2005
    #5
  6. 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
     
    XXXXXX.working.in.my.blood, Sep 10, 2005
    #6
  7. "XXXXXX.working.in.my.blood" <> 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.
    > 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.

    --
    Keith Thompson (The_Other_Keith) <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.
     
    Keith Thompson, Sep 10, 2005
    #7
  8. 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 <> 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" <> wrote:
    >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
    --
    Irrwahn Grausewitz ()
    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.
     
    Irrwahn Grausewitz, Sep 10, 2005
    #8
  9. sahu

    sahu Guest

    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.
     
    sahu, Sep 10, 2005
    #9
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Edwin Knoppert

    I'm lost, comming from VB to c#

    Edwin Knoppert, Feb 16, 2006, in forum: ASP .Net
    Replies:
    3
    Views:
    413
    Karl Seguin [MVP]
    Feb 16, 2006
  2. Miguel Manso

    Getting into Python, comming from Perl.

    Miguel Manso, Apr 24, 2005, in forum: Python
    Replies:
    7
    Views:
    353
    Peter Hansen
    Apr 27, 2005
  3. Mr. SweatyFinger
    Replies:
    2
    Views:
    2,090
    Smokey Grindel
    Dec 2, 2006
  4. ramesh
    Replies:
    0
    Views:
    285
    ramesh
    Aug 6, 2007
  5. dan_brown2k9

    Cool Book Comming Out

    dan_brown2k9, Jul 28, 2009, in forum: Java
    Replies:
    3
    Views:
    367
    Jan Paulsen
    Jul 29, 2009
Loading...

Share This Page