www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - Umm.. more array .init problems

reply "Jarrett Billingsley" <kb3ctd2 yahoo.com> writes:
Just downloaded DMD114 and found that the .init property of arrays sadly no 
longer initializes all the contents of the array.  I know, it's more 
consistent this way, but.. could there perhaps be a .clear property then?

Anyway I found the problem when I was looking for another way to quickly 
initialize the contents of an array.  I decided to see what exactly the 
.init property was returning, when I came across this.. bug?

import std.stdio;

void main()
{
 int[] a;
 a.length=2;
 a[0]=5;
 a[1]=10;
 int[] b=a.init;
 writefln(b[0],", ",b.length);
}

This causes an access violation on the line "int[] b=a.init".

However, if I change the first two lines like so:

import std.stdio;

void main()
{
 int[] a=new int[2];
 a[0]=5;
 a[1]=10;
 int[] b=a.init;
 writefln(b[0],", ",b.length);
}

It works. 
Feb 28 2005
next sibling parent "Jarrett Billingsley" <kb3ctd2 yahoo.com> writes:
Uhh.. is this not a problem?  I'd say a MAV for no reason is a problem. 
Mar 08 2005
prev sibling parent Thomas Kuehne <thomas-dloop kuehne.thisisspam.cn> writes:
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Jarrett Billingsley schrieb am Mon, 28 Feb 2005 17:02:56 -0500:
 Just downloaded DMD114 and found that the .init property of arrays sadly no 
 longer initializes all the contents of the array.  I know, it's more 
 consistent this way, but.. could there perhaps be a .clear property then?

 Anyway I found the problem when I was looking for another way to quickly 
 initialize the contents of an array.  I decided to see what exactly the 
 .init property was returning, when I came across this.. bug?

 import std.stdio;

 void main()
 {
  int[] a;
  a.length=2;
  a[0]=5;
  a[1]=10;
  int[] b=a.init;
  writefln(b[0],", ",b.length);
 }

 This causes an access violation on the line "int[] b=a.init".
I can't reproduce this on Linux. However your code has to segfault due to b[0] (b.length is 0). Added to DStress as http://dstress.kuehne.cn/run/array_initialization_05.d Thomas -----BEGIN PGP SIGNATURE----- iD8DBQFCOo5+3w+/yD4P9tIRAoFrAJ48Rdq2NQm49fM3iIE7xusotYI0HACgqPDn pRhdL+vgMEo/NOoXP2OoE+k= =EKoq -----END PGP SIGNATURE-----
Mar 18 2005