View previous topic :: View next topic |
Author |
Message |
Shawn Liu
Joined: 09 Mar 2005 Posts: 104 Location: Shanghai, China
|
Posted: Wed Mar 30, 2005 9:53 pm Post subject: traps in SWT and D. Share your know-how |
|
|
Some SWT source code needs to be carefully checked when porting to D. There are many traps than we expected. D has some difference from C/C++ programming. Some minutia may lead us to unsolvable.
Here is a place for us to share our experiences to benefit the DWT group and the DWT users.
Post your codes here and share the know-how. Thanks!
Shawn Liu
Last edited by Shawn Liu on Wed Mar 30, 2005 10:23 pm; edited 1 time in total |
|
Back to top |
|
|
Shawn Liu
Joined: 09 Mar 2005 Posts: 104 Location: Shanghai, China
|
Posted: Wed Mar 30, 2005 10:00 pm Post subject: reserved keyword "length" for arrays |
|
|
When "length" in brackets of an array, it means the length of the array. If the length is declared as a var. The value is overrided.
Code: |
e.g. SWT code:
Shell.findBrush(int pixel){
int[] brushes = new int[4];
...
int length = brushes.length;
int hBrush = brushes [--length];
if (hBrush != 0) OS.DeleteObject(hBrush);
System.arraycopy (brushes, 0, brushes, 1, length);
brushes [0] = hBrush = OS.CreateSolidBrush (pixel);
return hBrush;
} |
We may think, the "length" value introduced to System.arraycopy is 3. Actually it's 4 !
The sentence "brushes [--length]" didn't change the value of "length". Actually, the var "length" in "int length" and "brushes[--length]" are two different variables.
Check display.d carefully, there are so many variables named "length" !!!
Now D has a keyword "$" for arrays to represent the length of array (only in brackets). "length" maybe deprecated in the future. But currently it is a real trap!
Last edited by Shawn Liu on Wed Mar 30, 2005 11:18 pm; edited 1 time in total |
|
Back to top |
|
|
Shawn Liu
Joined: 09 Mar 2005 Posts: 104 Location: Shanghai, China
|
Posted: Wed Mar 30, 2005 10:21 pm Post subject: continued |
|
|
continued
the source in DWT, System.arraycopy()
Code: |
public static void arraycopy(Object[] src, int srcPos, Object[] dest, int destPos, int length) {
dest[destPos..(destPos+length)] = src[srcPos..(srcPos+length)];
} |
The introduce variable "length" is overrided to the two array's current length in the function body. DMD will complains that the size is not match for array operation since the two "length" in dest and src may be not equal. And this may cause array bounds check error.
We should change the name of that variable like this
Code: |
public static void arraycopy(Object[] src, int srcPos, Object[] dest, int destPos, int len) {
dest[destPos..(destPos+len)] = src[srcPos..(srcPos+len)];
} |
BTW, the code posted in previous message, Shell.findBrush(int pixel), will cause a slice overlapping error.
Actually it is
Code: |
brushes[1..4] = brushes[0..3];
|
which cause overlapping error. |
|
Back to top |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|