Newbie question: Why does this work?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Philipp.Weissenbacher@gmail.com

    Newbie question: Why does this work?

    Hi all!
    This is most certainly a total newbie question, but why doesn't the
    following code cause a segfault?

    void insertion_sort( int a[], int length)
    {
    int i;
    for (i=0; i < length; i++)
    {
    int j, v = a[i];
    for (j = i - 1; j >= 0; j--)
    {
    if (a[j] <= v) {
    cout << a[j] << " <= " << v << endl;
    break;
    }
    a[j + 1] = a[j];
    }
    a[j + 1] = v;
    }
    }

    IMHO the segfault should occur at line 9 as on the first pass through
    the first for-loop i = o ergo j is -1 causing a[j] to provoke a
    segfault.

    Thanks in advance and excuse me for asking such a dumb question
  • Richard Heathfield

    #2
    Re: Newbie question: Why does this work?

    Philipp.Weissen bacher@gmail.co m said:
    Hi all!
    This is most certainly a total newbie question, but why doesn't the
    following code cause a segfault?
    >
    void insertion_sort( int a[], int length)
    {
    int i;
    for (i=0; i < length; i++)
    {
    int j, v = a[i];
    for (j = i - 1; j >= 0; j--)
    {
    if (a[j] <= v) {
    This isn't broken because, first time through, i = 0, so j becomes i - 1 =
    -1, which is not >= 0, so the loop body is skipped completely.
    cout << a[j] << " <= " << v << endl;
    Just for the record, this is a C newsgroup, not a C++ newsgroup.

    <snip>

    --
    Richard Heathfield <http://www.cpax.org.uk >
    Email: -http://www. +rjh@
    Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
    "Usenet is a strange place" - dmr 29 July 1999

    Comment

    • santosh

      #3
      Re: Newbie question: Why does this work?

      Philipp.Weissen bacher@gmail.co m wrote:
      Hi all!
      This is most certainly a total newbie question, but why doesn't the
      following code cause a segfault?
      >
      void insertion_sort( int a[], int length)
      {
      int i;
      for (i=0; i < length; i++)
      {
      int j, v = a[i];
      for (j = i - 1; j >= 0; j--)
      {
      if (a[j] <= v) {
      cout << a[j] << " <= " << v << endl;
      break;
      }
      a[j + 1] = a[j];
      }
      a[j + 1] = v;
      }
      }
      >
      IMHO the segfault should occur at line 9 as on the first pass through
      the first for-loop i = o ergo j is -1 causing a[j] to provoke a
      segfault.
      >
      Thanks in advance and excuse me for asking such a dumb question
      The second for loop will be entered only when it's condition is true,
      i.e., only when j is >= 0. This will not happen after the first
      iteration of the outer loop. During the second iteration of the outer
      loop, j will be initialised to 0 so the code under the inner for loop
      will be executed.

      Comment

      • Walter Roberson

        #4
        Re: Newbie question: Why does this work?

        In article <642e48fe-b105-4178-bac2-c5a49c8e466c@a2 3g2000hsc.googl egroups.com>,
        Philipp.Weissen bacher@gmail.co m <Philipp.Weisse nbacher@gmail.c omwrote:
        >This is most certainly a total newbie question, but why doesn't the
        >following code cause a segfault?
        >void insertion_sort( int a[], int length)
        >{
        int i;
        for (i=0; i < length; i++)
        {
        > int j, v = a[i];
        > for (j = i - 1; j >= 0; j--)
        > {
        > if (a[j] <= v) {
        > cout << a[j] << " <= " << v << endl;
        The above line will not compile, as you have not defined a variable
        named 'cout', nor a variable named 'endl'. Furthermore, left-shifting
        a pointer by a value, or a value by a pointer, is not a defined
        operation in C. Perhaps in copying out the program for the posting,
        you accidently pasted in a line from another window in which you
        had something dealing with some other programming language such as
        Rogaine ?

        > break;
        > }
        > a[j + 1] = a[j];
        > }
        > a[j + 1] = v;
        }
        >}
        >IMHO the segfault should occur at line 9 as on the first pass through
        >the first for-loop i = o ergo j is -1 causing a[j] to provoke a
        >segfault.
        When i = 0, and you start the for (j = i - 1; j >= 0; j--) loop,
        then j -will- be initialized to -1, but the loop condition j >= 0
        will be evaluated before any trips are taken through the loop, and will
        be found to be false, so the loop will not be executed. The j >= 0
        protects the loop from executing when j is not at least 0.
        --
        "Man's life is but a jest,
        A dream, a shadow, bubble, air, a vapor at the best."
        -- George Walter Thornbury

        Comment

        • Default User

          #5
          Re: Newbie question: Why does this work?

          Philipp.Weissen bacher@gmail.co m wrote:
          IMHO the segfault should occur at line 9 as on the first pass through
          the first for-loop i = o ergo j is -1 causing a[j] to provoke a
          segfault.
          Even if you had accessed outside the boundaries, that's undefined
          behavior. There is no defined behavior for undefined behavior. That
          includes segfaults or any other behavior.





          Brian

          Comment

          • Charlton Wilbur

            #6
            Re: Newbie question: Why does this work?

            >>>>"P" == Philipp Weissenbacher@g mail com
            >>>><Philipp.We issenbacher@gma il.comwrites:
            PHi all! This is most certainly a total newbie question, but why
            Pdoesn't the following code cause a segfault?

            Because segfaults are a bonus, not a requirement.

            Charlton


            --
            Charlton Wilbur
            cwilbur@chromat ico.net

            Comment

            • Philipp.Weissenbacher@gmail.com

              #7
              Re: Newbie question: Why does this work?

              On 16 Mai, 21:01, Charlton Wilbur <cwil...@chroma tico.netwrote:
              >>>"P" == Philipp Weissenbacher@g mail com
              >>><Philipp.Wei ssenbac...@gmai l.comwrites:
              >
              PHi all! This is most certainly a total newbie question, but why
              Pdoesn't the following code cause a segfault?
              >
              Because segfaults are a bonus, not a requirement.
              >
              Charlton
              >
              --
              Charlton Wilbur
              cwil...@chromat ico.net
              At first thanks for all the answers. And yes I'm actually using C++; I
              just missed some couts. Sorry for that one.
              @Roberson: I always thought a for loop does at lest one iteration
              before checking the condition. Gotta reread that part ...

              Comment

              • Walter Roberson

                #8
                Re: Newbie question: Why does this work?

                In article <a235703d-62b9-4378-9518-8b15fc8505a5@d7 7g2000hsb.googl egroups.com>,
                Philipp.Weissen bacher@gmail.co m <Philipp.Weisse nbacher@gmail.c omwrote:
                >@Roberson: I always thought a for loop does at lest one iteration
                >before checking the condition. Gotta reread that part ...
                No, the only iteration form that does at least one iteration before
                checking the condition is "do until"

                --
                "I think Walter's legacy will be that of a man with a God-given
                ability that got the most out of it at every possible chance."
                -- Eddie Payton

                Comment

                • Keith Thompson

                  #9
                  Re: Newbie question: Why does this work?

                  "Philipp.Weisse nbacher@gmail.c om" <Philipp.Weisse nbacher@gmail.c omwrites:
                  This is most certainly a total newbie question, but why doesn't the
                  following code cause a segfault?
                  >
                  void insertion_sort( int a[], int length)
                  {
                  int i;
                  for (i=0; i < length; i++)
                  {
                  int j, v = a[i];
                  for (j = i - 1; j >= 0; j--)
                  {
                  if (a[j] <= v) {
                  cout << a[j] << " <= " << v << endl;
                  printf("%d <= %d\n", a[j], v); /* This is comp.lang.c. */
                  break;
                  }
                  a[j + 1] = a[j];
                  }
                  a[j + 1] = v;
                  }
                  }
                  >
                  IMHO the segfault should occur at line 9 as on the first pass through
                  the first for-loop i = o ergo j is -1 causing a[j] to provoke a
                  segfault.
                  Here's something you could have tried that probably would have avoided
                  the need to post the question:

                  By inserting a printf call at the beginning of the for loop:

                  printf("j = %d\n", j);

                  you could have seen that j never has the value -1 (which would leave
                  you to fix the logic problem that prevents the loop from being
                  executed at all).

                  Or you could do the equivalent in a debugger; the details of how to do
                  that are off-topic here, but should be in your debugger's documentation.

                  In any case, even if you did evaluate a[j] with j == -1, there's no
                  guarantee that it would cause a seg fault, or any other particular
                  result. The behavior is simply undefined (i.e., the C standard says
                  absolutely nothing about what might happen). It could cause a seg
                  fault, it could quietly access some piece of memory outside the array,
                  it could clobber something that causes your program to crash later.
                  It can't *really* make demons fly out of your nose (as the old joke
                  here goes), but if it did, it wouldn't be a violation of the C
                  standard.

                  --
                  Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
                  Nokia
                  "We must do something. This is something. Therefore, we must do this."
                  -- Antony Jay and Jonathan Lynn, "Yes Minister"

                  Comment

                  • Keith Thompson

                    #10
                    Re: Newbie question: Why does this work?

                    roberson@ibd.nr c-cnrc.gc.ca (Walter Roberson) writes:
                    In article <a235703d-62b9-4378-9518-8b15fc8505a5@d7 7g2000hsb.googl egroups.com>,
                    Philipp.Weissen bacher@gmail.co m <Philipp.Weisse nbacher@gmail.c omwrote:
                    >
                    >>@Roberson: I always thought a for loop does at lest one iteration
                    >>before checking the condition. Gotta reread that part ...
                    >
                    No, the only iteration form that does at least one iteration before
                    checking the condition is "do until"
                    Or, if you happen to be programming in C <OT>or C++</OT>, "do while".
                    <OT>Pascal has "repeat until"; perhaps that's what you were thinking
                    of.</OT>

                    --
                    Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
                    Nokia
                    "We must do something. This is something. Therefore, we must do this."
                    -- Antony Jay and Jonathan Lynn, "Yes Minister"

                    Comment

                    • Walter Roberson

                      #11
                      Re: Newbie question: Why does this work?

                      In article <lnd4nmghpa.fsf @nuthaus.mib.or g>,
                      Keith Thompson <kst-u@mib.orgwrote:
                      >roberson@ibd.n rc-cnrc.gc.ca (Walter Roberson) writes:
                      >In article <a235703d-62b9-4378-9518-8b15fc8505a5@d7 7g2000hsb.googl egroups.com>,
                      >No, the only iteration form that does at least one iteration before
                      >checking the condition is "do until"
                      >Or, if you happen to be programming in C <OT>or C++</OT>, "do while".
                      ><OT>Pascal has "repeat until"; perhaps that's what you were thinking
                      >of.</OT>
                      Nah, I just have a thick head today :(
                      --
                      'Roberson' is my family name; my given name is 'Walter'.

                      Comment

                      • nembo kid

                        #12
                        Re: Newbie question: Why does this work?

                        Philipp.Weissen bacher@gmail.co m ha scritto:
                        cout <<
                        Bleah... ;-)

                        Comment

                        • Philipp.Weissenbacher@gmail.com

                          #13
                          Re: Newbie question: Why does this work?

                          On 16 Mai, 21:52, rober...@ibd.nr c-cnrc.gc.ca (Walter Roberson) wrote:
                          In article <lnd4nmghpa.... @nuthaus.mib.or g>,
                          Keith Thompson <ks...@mib.orgw rote:
                          >
                          rober...@ibd.nr c-cnrc.gc.ca (Walter Roberson) writes:
                          In article <a235703d-62b9-4378-9518-8b15fc850...@d7 7g2000hsb.googl egroups.com>,
                          No, the only iteration form that does at least one iteration before
                          checking the condition is "do until"
                          Or, if you happen to be programming in C <OT>or C++</OT>, "do while".
                          <OT>Pascal has "repeat until"; perhaps that's what you were thinking
                          of.</OT>
                          >
                          Nah, I just have a thick head today :(
                          --
                          'Roberson' is my family name; my given name is 'Walter'.
                          @Walter: Thought this would be more polite. I'm a German native-
                          speaker and we have something called T-V distinction. The English
                          counterpart to "sie" appears to be calling someone with his family
                          name (at least for me) whereas calling someone by his given name
                          indicates a much closer relationship.

                          Comment

                          Working...