DMD 0.164: Possible bug in order of evaluation

Klaus Oberhofer oberhofer at users.sourceforge.net
Fri Aug 18 02:18:10 PDT 2006


While porting some C-Code to D I had a possible bug that has to do with 
the order of evaluation of the post increment operator. 
The following C sample

<c-code>
#include <memory.h>
#include <stdio.h>

void main()
{
  unsigned short testarray_a[17];
  unsigned short testarray_b[17];

  unsigned short start  = 0;

  int i;

  memset(testarray_a, 0, sizeof(testarray_a));
  memset(testarray_b, 0, sizeof(testarray_a));
  
  i = start;
  while (i <= 16)
  {
    testarray_a[i] = 1U << (16 - i);
    i++;
  }

  i = start;
  while (i <= 16)
  {
    testarray_b[i++] = 1U << (16 - i);
  }

  printf("a:");
  for (i = 1; i <= 16; i++)
    printf("%i ", testarray_a[i]);
  printf("\n");

  printf("b:");
  for (i = 1; i <= 16; i++)
    printf("%i ", testarray_b[i]);
  printf("\n");
}
</c-code>

compiled with DMC and VC7 creates the following output:

a:32768 16384 8192 4096 2048 1024 512 256 128 64 32 16 8 4 2 1 
b:32768 16384 8192 4096 2048 1024 512 256 128 64 32 16 8 4 2 1 

An equivalent D code

<d-code>
module testarray.main;

void main()
{
  ushort testarray_a[17];
  ushort testarray_b[17];

  int i;
  ushort start = 0;

  i = start;
  while (i <= 16)
  {
    testarray_a[i] = 1U << (16 - i);
    i++;
  }

  i = start;
  while (i <= 16)
  {
    testarray_b[i++] = 1U << (16 - i);
  }

  printf("a:");
  for (i = 1; i <= 16; i++)
    printf("%i ", testarray_a[i]);
  printf("\n");

  printf("b:");
  for (i = 1; i <= 16; i++)
    printf("%i ", testarray_b[i]);
  printf("\n");
}
</d-code>

compiled with DMD 0.164 produces:

a:32768 16384 8192 4096 2048 1024 512 256 128 64 32 16 8 4 2 1 
b:16384 8192 4096 2048 1024 512 256 128 64 32 16 8 4 2 1 0 

Could someone confirm this as a bug ?



More information about the Digitalmars-d-bugs mailing list