[vox-tech] loop never exits!

Brian Lavender brian at brie.com
Wed Apr 21 13:05:26 PDT 2010


Well, it seems to me that to reverse a array in C, you are going to have
to use a counter and decrement it. And, since C uses zero indexed
arrays, it certainly seems to leave me in a quandry whether to use
signed or unsigned. We know that an array index should always be zero or
greater. Yet, if we use a simple for loop, one has to test for an exit
condition. So, what's the solution??? Is it just make the index value
"i" signed?

Below is some Pascal code that goes in reverse. You can see, the reverse
loop is much cleaner and easier to understand using the following for
loop to go in reverse. 

for count := MaxIndex downto 1 do
begin
  // reference array elements here
end;

Is there a way to do this in C that is so clear? I believe that Pascal
even has a flag to tell if your array index is out bounds. I am not sure
how to enable that though.

I found this article. I haven't read it yet, but it certainly appears
interesting.

http://www.doc.ic.ac.uk/~phjk/BoundsChecking.html


program hello;

{$mode objfpc}{$H+}

uses
  {$IFDEF UNIX}{$IFDEF UseCThreads}
  cthreads,
  {$ENDIF}{$ENDIF}
  Classes,
  sysutils
  { you can add units after this };

{$IFDEF WINDOWS}{$R hello.rc}{$ENDIF}

const MaxIndex = 5;
type
Index = 1 .. MaxIndex;
MyArray1 = array[Index] of integer;

var
   count: Integer;
   nums: MyArray1;

begin
  nums[1] := 12;
  nums[2] := 24;
  nums[3] := 36;
  nums[4] := 48;
  nums[5] := 60;

  writeln('The elements in reverse order');

  for count := MaxIndex downto 1 do
  begin
    writeln(Format('element %d is %d',[Count, nums[Count]]) );
  end;

  readln;

end.

On Wed, Apr 21, 2010 at 12:20:14PM -0700, Jeff Newmiler wrote:
> This desire for compiler warnings that DWIM rather than DWIS have been around for a long time, and the usual reply from the compiler coders has been along the lines of "go run lint if you need help interpreting your legal code".
> 
> Given your use of unsigned variables, replacing "i >=0" with "1" is exactly what optimizers are supposed to do., because writing code more verbosely than necessary is a documentation decision that only the programmer can make. 
> 
> Brian Lavender <brian at brie.com> wrote:
> 
> >I told the compiler to go through a loop while a certain expression
> >evaluates to true. When the expression evalates to false, the loop
> >should terminate. If I had provided no expression, or I had provided
> >true, or some value that would always evaluate to true, then I would
> >expect the compiler to do as I tell it.
> >
> >I have provided an expression for the loop to terminate that could
> >evaluate to false, yet will never evaluate to false.
> >
> >This determination can be done through static analysis. Hence,
> >I am still convinced that the compiler should issue a warning. Yet,
> >I am still looking for a contradiction to my theory.
> >
> >
> >
> >
> >On Tue, Apr 20, 2010 at 08:30:42PM -0700, Matthew Holland wrote:
> >> It's not the compiler's job to tell you what you want, it's your job
> >> to tell it!  The -Wall flag doesn't mean, "warn me about every
> >> possible stupid construct I can come up with."
> >> 
> >> In all seriousness, if you don't get over using a "greater than or
> >> equal" construct for loop termination, you're going to be in a world
> >> of hurt if STL containers ever cross your transom.  Just don't do
> >> that, and everything will be fine.
> >> 
> >> Matt
> >> 
> >> On Tue, Apr 20, 2010 at 7:44 PM, Brian Lavender <brian at brie.com> wrote:
> >> > On Tue, Apr 20, 2010 at 06:54:33PM -0700, Bill Broadley wrote:
> >> >> On 04/20/2010 06:37 PM, Brian Lavender wrote:
> >> >> > Our new guy (forget his name, doh!) and I figured out the problem with
> >> >> > my loop that would count down, but not terminate. Turns out I was using
> >> >> > an unsigned integer for my counter in my for loop and it is always
> >> >> > greater than zero (Example 1).
> >> >>
> >> >> No, it's not always greater than zero.  Your test says i>=0 so if it's
> >> >> greater than or equal to zero it continues.  Seems like you want i>0.
> >> >
> >> > Sorry, I meant to say it's always greater than or equal than zero. zero
> >> > minus 1 is 4294967295 (0xffffffff) on a 32 bit machine. It can never go
> >> > negative because it is unsigned and the loop will never terminate. Thus,
> >> > I am thinking that the compiler could catch this due to the fact that i
> >> > is unsigned. I wanted to print out the reverse of an array.
> >> >
> >> > If you run the following, the loop will never terminate.
> >> >
> >> > #include <stdio.h>
> >> >
> >> > int main() {
> >> >  int a[] = {5,6,8,3,4};
> >> >  unsigned int i;
> >> >
> >> >
> >> >  for (i= (sizeof(a) -1)/sizeof(int) ; i >= 0; i--) {
> >> >    printf("%d\n",a[i]);
> >> >  }
> >> >
> >> >  return 0;
> >> > }
> >> >
> >> >>
> >> >> > Funny thing is that -Wall didn't catch this. Seems that -Wall could
> >> >> > catch this assuming that we want to loop to terminate. Any thoughts?
> >> >>
> >> >> Seems strange, but legal to do what you wanted.
> >> >>
> >> >> > Say the compiler gave a warning, would that mess up the "for (;;)"
> >> >> > construct shown in Example 2?
> >> >> >
> >> >> > brian
> >> >> >
> >> >> > // Example 1
> >> >> > // Loop never terminates
> >> >> > #include<stdio.h>
> >> >> >
> >> >> > int main() {
> >> >> >    unsigned int i, num=50;
> >> >> >
> >> >> >
> >> >> >    for (i= num ; i>= 0; i--) {
> >> >> >      printf("%u\n",i);
> >> >> >    }
> >> >> >
> >> >> >    return 0;
> >> >> > }
> >> >> >
> >> >> > // Example 2
> >> >> > // Purposely never terminates
> >> >> > #include<stdio.h>
> >> >> >
> >> >> > int main() {
> >> >> >    for (;;) {
> >> >> >      printf("Hello forever\n");
> >> >> >     }
> >> >> >     return 0;
> >> >> > }
> >> >
> >> >
> >> >
> >> > --
> >> > Brian Lavender
> >> > http://www.brie.com/brian/
> >> >
> >> > "For every complex problem there is an answer that is clear, simple, and wrong."
> >> > - H. L. Mencken
> >> > _______________________________________________
> >> > vox-tech mailing list
> >> > vox-tech at lists.lugod.org
> >> > http://lists.lugod.org/mailman/listinfo/vox-tech
> >> >
> >> _______________________________________________
> >> vox-tech mailing list
> >> vox-tech at lists.lugod.org
> >> http://lists.lugod.org/mailman/listinfo/vox-tech
> >
> >-- 
> >Brian Lavender
> >http://www.brie.com/brian/
> >
> >"For every complex problem there is an answer that is clear, simple, and wrong."
> >- H. L. Mencken 
> >_______________________________________________
> >vox-tech mailing list
> >vox-tech at lists.lugod.org
> >http://lists.lugod.org/mailman/listinfo/vox-tech
> _______________________________________________
> vox-tech mailing list
> vox-tech at lists.lugod.org
> http://lists.lugod.org/mailman/listinfo/vox-tech

-- 
Brian Lavender
http://www.brie.com/brian/

"For every complex problem there is an answer that is clear, simple, and wrong."
- H. L. Mencken 


More information about the vox-tech mailing list