August 08, 2006

I wrote my PCRE D wrapper module a few days ago, It can be found at
http://icube.freezope.org/pcred/
http://icube.freezope.org/pcred/pcred-0.1.zip
And I got Access Violation when run the sample which compiled with
"build -release -O -clean sample.d", but everything is OK when i remove
the -O flag.
This can be resolved by using volatile synchronized or change some
expression, see below.
But i'd like to confirm, is it really a dmd bug or somewhat?

To find out where the problem occurs, I wrote a small block of codes which
demonstrates the problem.
the writefln statements show the difference between the compilation with -O
or not.

with DMD 0.155/0.163


import std.stdio;
import std.process;

char[] replace(char[] subject) {
  char[] s = new char[subject.length];
  int lng = 0;
  char[] sa;
  int lngnew;
  int i = 0;
  while(i<10) {
   i++;
   sa = "repxxxxxxxxxxxx";
   lngnew = lng + sa.length;
   if(lngnew > s.length) s.length = lngnew * 2; // expand size
   writefln(lng, \t, lngnew, \t, s.length, \t, sa.length);
   s[lng .. lngnew] = sa[0 .. $];
   lng = lngnew;

   //volatile{//OK

   sa = "repzzzzzzzzzzzzzzzzzzzzzz";
   //lngnew = lng-1 + sa.length+1;//OK
   //synchronized lngnew = lng + sa.length;//OK
   //lngnew += sa.length;//OK
   lngnew = lng + sa.length;// 2*sa.length?
   if(lngnew > s.length) s.length = lngnew * 2; // expand size
   writefln(lng, \t, lngnew, \t, s.length, \t, sa.length);
   s[lng .. lngnew] = sa[0 .. $];
   lng = lngnew;

   //}

  }
  return s[0 .. lng];
}

void demo() {
 int lng = 0;
 int lngnew = 0;
 int i = 0;
 char[] sa = "xxxx";//it seem to be around the Dynamic Array's length
property.

 // class SA {
  // int _a;
  // this(int a) {_a = a;}
  // int length() {return _a;}
 // }
 // SA sa = new SA(4);//OK
 while(i<10) {
  i++;
  lngnew = lng + sa.length;
  writefln(lng, \t, lngnew, \t, sa.length);
  lng = lngnew;

  lngnew = lng + sa.length;
  writefln(lng, \t, lngnew, \t, sa.length);
  lng = lngnew;
 }
}

int main(char[][] args) {
 try {
  demo();
  writefln(replace("012345678901234567890"));
 } catch(Exception e) {
  writefln(e.msg);
 }
 system("pause");
 return 0;
}


August 11, 2006
Please post this in the D Bugzilla: http://d.puremagic.com/bugzilla/

Thanks.

icee wrote:
> I wrote my PCRE D wrapper module a few days ago, It can be found at http://icube.freezope.org/pcred/
> http://icube.freezope.org/pcred/pcred-0.1.zip
> And I got Access Violation when run the sample which compiled with "build -release -O -clean sample.d", but everything is OK when i remove the -O flag.
> This can be resolved by using volatile synchronized or change some expression, see below.
> But i'd like to confirm, is it really a dmd bug or somewhat?
> 
> To find out where the problem occurs, I wrote a small block of codes which demonstrates the problem.
> the writefln statements show the difference between the compilation with -O or not.
> 
> with DMD 0.155/0.163
> 
> 
> import std.stdio;
> import std.process;
> 
> char[] replace(char[] subject) {
>   char[] s = new char[subject.length];
>   int lng = 0;
>   char[] sa;
>   int lngnew;
>   int i = 0;
>   while(i<10) {
>    i++;
>    sa = "repxxxxxxxxxxxx";
>    lngnew = lng + sa.length;
>    if(lngnew > s.length) s.length = lngnew * 2; // expand size
>    writefln(lng, \t, lngnew, \t, s.length, \t, sa.length);
>    s[lng .. lngnew] = sa[0 .. $];
>    lng = lngnew;
> 
>    //volatile{//OK
> 
>    sa = "repzzzzzzzzzzzzzzzzzzzzzz";
>    //lngnew = lng-1 + sa.length+1;//OK
>    //synchronized lngnew = lng + sa.length;//OK
>    //lngnew += sa.length;//OK
>    lngnew = lng + sa.length;// 2*sa.length?
>    if(lngnew > s.length) s.length = lngnew * 2; // expand size
>    writefln(lng, \t, lngnew, \t, s.length, \t, sa.length);
>    s[lng .. lngnew] = sa[0 .. $];
>    lng = lngnew;
> 
>    //}
> 
>   }
>   return s[0 .. lng];
> }
> 
> void demo() {
>  int lng = 0;
>  int lngnew = 0;
>  int i = 0;
>  char[] sa = "xxxx";//it seem to be around the Dynamic Array's length property.
> 
>  // class SA {
>   // int _a;
>   // this(int a) {_a = a;}
>   // int length() {return _a;}
>  // }
>  // SA sa = new SA(4);//OK
>  while(i<10) {
>   i++;
>   lngnew = lng + sa.length;
>   writefln(lng, \t, lngnew, \t, sa.length);
>   lng = lngnew;
> 
>   lngnew = lng + sa.length;
>   writefln(lng, \t, lngnew, \t, sa.length);
>   lng = lngnew;
>  }
> }
> 
> int main(char[][] args) {
>  try {
>   demo();
>   writefln(replace("012345678901234567890"));
>  } catch(Exception e) {
>   writefln(e.msg);
>  }
>  system("pause");
>  return 0;
> } 
> 
>