February 11, 2004 [Bug?] Casting, rectangular array to dynamic array of array | ||||
---|---|---|---|---|
| ||||
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 */ |
February 14, 2004 Re: [Bug?] Casting, rectangular array to dynamic array of array | ||||
---|---|---|---|---|
| ||||
Posted in reply to berupon | 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...
|
Copyright © 1999-2021 by the D Language Foundation