www.digitalmars.com         C & C++   DMDScript  

D - Weird int/long arithmetic errors (overwriting memory?)

reply DDevil <ddevil functionalfuture.com> writes:
I ran into this when porting the sieve shootout benchmark.

Here is the code with comments added.  When I run this I get an array 
bounds exception.  This code works in C.  Maybe it's just something I 
don't understand about D, but it's a very subtle error.  I had to change 
two of the variables to int's instead of long's to fix the error.  Since 
there are no pointers being used it's weird that this error could even 
occur.

Unfortunatly I was not able to isolate the problem any more than this. 
If I take out any of the code then everything starts working.

//************************************************************

import string;

int main(char[][] args)
{
    int n = string.atoi(args[1]);
    static char flags[8192 + 1];
    long i, k; //*** THESE MUST BE CHANGED TO int TO FIX THE PROBLEM
    int count = 0;

    try
    {
       while (n--)
       {
          count = 0;

          for (i = 2; i <= 8192; i++)
             flags[i] = 1;

          for (i = 2; i <= 8192; i++)
          {
             if (flags[i])
             {
                //*** WHEN i==123 AND k==0 THIS WILL TRY TO ACCESS
                //    ELEMENT 8241 (?!) AND THROW EXCEPTION (SHOULD
                //    NOT HAPPEN?)
                for (k = i+i; k <= 8192; k += i)
                   flags[k] = 0;

                count++;
             }
          }
       }

       printf("Count: %d\n", count);
    }
    catch
    {
       printf("Exception: %d\n", k);
    }

    return(0);
}

//************************************************************

Is it just me, or what?

--
// DDevil
Mar 08 2003
parent reply Burton Radons <loth users.sourceforge.net> writes:
DDevil wrote:
 I ran into this when porting the sieve shootout benchmark.
 
 Here is the code with comments added.  When I run this I get an array 
 bounds exception.  This code works in C.  Maybe it's just something I 
 don't understand about D, but it's a very subtle error.  I had to change 
 two of the variables to int's instead of long's to fix the error.  Since 
 there are no pointers being used it's weird that this error could even 
 occur.
long is a 64-bit type in D! Use int. Otherwise, your problem isn't reproduceable here. Are you using DMD 0.58?
Mar 08 2003
parent reply DDevil <ddevil functionalfuture.com> writes:
Burton Radons wrote:
 long is a 64-bit type in D!  Use int.  Otherwise, your problem isn't 
 reproduceable here.  Are you using DMD 0.58?
I'm using DMD 0.59. I might give 0.58 a shot to see if I still get the error. I understand that it's a 64-bit type, but I would think that it should convert to/from 32-bit types or if not then the compiler should issue an error (?). Interesting that it can't be reproduced. Are you using DMD 0.59? Thanks! -- // DDevil
Mar 08 2003
parent Burton Radons <loth users.sourceforge.net> writes:
DDevil wrote:
 Burton Radons wrote:
 
 long is a 64-bit type in D!  Use int.  Otherwise, your problem isn't 
 reproduceable here.  Are you using DMD 0.58?
I'm using DMD 0.59. I might give 0.58 a shot to see if I still get the error. I understand that it's a 64-bit type, but I would think that it should convert to/from 32-bit types or if not then the compiler should issue an error (?).
It does, and implicitly. This is a code generation error.
 Interesting that it can't be reproduced.  Are you using DMD 0.59?
Walter didn't announce 0.59, so I didn't know about it. I confirm that 0.59 causes your code to crash when compiling it with "dmd -O foo.d"; "dmd foo.d" produces the correct code.
Mar 08 2003