www.digitalmars.com         C & C++   DMDScript  

D - [Bug?] Casting, rectangular array to dynamic array of array

reply berupon <berupon_member pathlink.com> writes:
It would be great if I could cast rectangular array to dynamic array of array.
Below is test code.

void printArray(int[] arr)
{
for (int i=0; i<arr.length; ++i) {
printf("[%d] = %d\n", i, arr[i]);
}
}

int main(char[][] arg)
{
static int[2] arr1 = [1, 2];
static int[] arr2 = [1, 2, 3];
printArray(arr2);
printf("\n");

// Casting
// (DynamicArray) StaticArray
// works right
arr2 = cast(int[]) arr1;
printArray(arr2);
printf("\n");

static int arra1[3][2] =
[
[1, 2],
[3, 4],
[5, 5]
];

int arr[];
arr = cast(int[]) arra1[0];
printArray(arr);
printf("\n");

// Casting
// (DynamicArrayOfArray) RectangularArray
// produces odd array
int arra[][];
arra = cast(int[][]) arra1; // Compile passes
printArray(arra[0]); // Access Violation Error

return 0;
}

/*

[0] = 1
[1] = 2
[2] = 3

[0] = 1
[1] = 2

[0] = 1
[1] = 2

Error: Access Violation

*/
Feb 10 2004
parent "Carlos Santander B." <carlos8294 msn.com> writes:
berupon wrote:
 It would be great if I could cast rectangular array to dynamic array of array.
 Below is test code.
 
 void printArray(int[] arr)
 {
 for (int i=0; i<arr.length; ++i) {
 printf("[%d] = %d\n", i, arr[i]);
 }
 }
 
 int main(char[][] arg)
 {
 static int[2] arr1 = [1, 2];
 static int[] arr2 = [1, 2, 3];
 printArray(arr2);
 printf("\n");
 
 // Casting
 // (DynamicArray) StaticArray
 // works right
 arr2 = cast(int[]) arr1;
 printArray(arr2);
 printf("\n");
 
 static int arra1[3][2] =
 [
 [1, 2],
 [3, 4],
 [5, 5]
 ];
 
 int arr[];
 arr = cast(int[]) arra1[0];
 printArray(arr);
 printf("\n");
 
 // Casting
 // (DynamicArrayOfArray) RectangularArray
 // produces odd array
 int arra[][];
 arra = cast(int[][]) arra1; // Compile passes
 printArray(arra[0]); // Access Violation Error
 
 return 0;
 }
 
 /*
 
 [0] = 1
 [1] = 2
 [2] = 3
 
 [0] = 1
 [1] = 2
 
 [0] = 1
 [1] = 2
 
 Error: Access Violation
 
 */
 
 
 
I wouldn't call it a bug, but something not implemented. It's been suggested before and Walter gave explanations which I really can't remember. I believe their internals are different. But I dunno...
Feb 13 2004