Author |
Message |
robertg
Joined: Oct 23, 2010 Posts: 4 Location: Fort Lauderdale, FL
|
Posted: Sat Mar 26, 2011 7:40 am Post subject:
2d dynamic array Subject description: 1 dim dynamic other fixed |
|
|
Hello
Trying to create a dynamic array structure which represents a record like structure of three variables. Basic idea is that increasing size of the first array dim adds a new row and the second dim represents vars of that row. The second dim doesn't grow in size.
a[1][3] - row
a[2][3] - next, etc.
I tried the various methods to resize arrays: size, << but I keep running into issues when assigning/ accessing elements. Is it possible to grow one dimension but keep the other dimension fixed? Are there specific guidelines when accessing the dynamic/ static dimensional array structure?
// code sample
// first row
int a[1][3];
<<< a.size() >>>; // this appears to return 1st dim size
<<< a[0].size() >>>; // this appears tor return 2nd dim size though syntax is not clear why would be the case
1 => a[0][0]; <<< a[0][0] >>>; //works
2 => a[0][1]; <<< a[0][1] >>>; // works
3 => a[0][2]; <<< a[0][2] >>>; // works
// second row
a.size() + 1 => a.size; // this appears to increase first dim size
<<< a.size() >>>;
<<< a[0].size() >>>;
4 => a[1][0]; <<< a[1][0] >>>; // Assignment errors start here after first dynamic row increase
5 => a[1][1]; <<< a[1][1] >>>;
6 => a[1][2]; <<< a[1][2] >>>;
Thanks - appreciated
Robert |
|
Back to top
|
|
|
robertg
Joined: Oct 23, 2010 Posts: 4 Location: Fort Lauderdale, FL
|
Posted: Sun Mar 27, 2011 5:41 am Post subject:
Subject description: problems with multi dim float arrays? |
|
|
Still working on this. I was able to get this to work with a multi dim int array but not (same code!) with a multi dim float array. Any assistance greatly appreciated!
// example 1 - works with dynamic multi dim int array
int a[0][0];
a << [1, 2, 3];
<<< "a.size(): ", a.size() >>>;
<<< "a[0].size(): ", a[0].size() >>>;
<<< "a[0][0]:", a[0][0] >>>;
<<< "a[0][1]:", a[0][1] >>>;
<<< "a[0][2]:", a[0][2] >>>;
a << [4, 5, 6];
<<< "a.size(): ", a.size() >>>;
<<< "a[0].size(): ", a[0].size() >>>;
<<< "a[1].size(): ", a[1].size() >>>;
<<< "a[1][0]:", a[1][0] >>>;
<<< "a[1][1]:", a[1][1] >>>;
<<< "a[1][2]:", a[1][2] >>>;
// example 2 - same thing crashes when type is changed to float
float a[0][0];
a << [1.0, 2.0, 3.0];
<<< "a.size(): ", a.size() >>>;
<<< "a[0].size(): ", a[0].size() >>>;
<<< "a[0][0]:", a[0][0] >>>;
<<< "a[0][1]:", a[0][1] >>>;
<<< "a[0][2]:", a[0][2] >>>;
// example 3 - this doesn't work either, it seems to only increase first array dim size but doesn't create slots for second dim
float a[1][3];
1.0 => a[0][0];
2.0 => a[0][1];
3.0 => a[0][2];
<<< "a.size(): ", a.size() >>>;
<<< "a[0].size(): ", a[0].size() >>>;
a.size() + 1 => a.size;
<<< "a.size(): ", a.size() >>>;
<<< "a[0].size(): ", a[0].size() >>>;
<<< "a[1].size(): ", a[1].size() >>>; // this crashes
4.0 => a[1][0]; // these assignments crash
5.0 => a[1][1];
6.0 => a[1][2];
Thanks |
|
Back to top
|
|
|
trzewiczek
Joined: Sep 24, 2010 Posts: 4 Location: Poland
|
Posted: Fri Apr 01, 2011 11:18 pm Post subject:
Subject description: Funny thing with float array size |
|
|
Hi,
It looks like a bug in the language. I tried to push thing to the wall and started to play stupid stuff with those arrays. Take a look
Code: | int a[0][0];
a << 1;
a << 2;
a << 3;
a << 4;
a << 5;
<<< "a.size()", a.size() >>>;
for( 0 => int i; i < a.size(); i++ )
{
<<< "a[", i, "] ::", a[i] >>>;
} |
The output is:
Code: | a.size() 5
a[ 0 ] :: 0x1
a[ 1 ] :: 0x2
a[ 2 ] :: 0x3
a[ 3 ] :: 0x4
a[ 4 ] :: 0x5
|
I know, I'm not fair, but...
This makes funny thing - it seems to assigns new addresses in memory for each array position. This is kind of cool - no problem with that (thow, it crashes ChucK after using it and "why would you like to do that?" ).
What's funny for me is what happens when you change int to float in this code. The output is:
Code: | a.size() 10
a[ 0 ] :: 0x0
a[ 1 ] :: 0x3ff00000
a[ 2 ] :: 0x0
a[ 3 ] :: 0x40000000
a[ 4 ] :: 0x0
a[ 5 ] :: 0x40080000
a[ 6 ] :: 0x0
a[ 7 ] :: 0x40100000
a[ 8 ] :: 0x0
a[ 9 ] :: 0x40140000 |
Array size is... 10!! OK - I'm not a computer sciencist, so maybe it ok, but for my uneducated soul it looks, like there is something wrong with float dynamic multidim arrays handling in the language.
I know it's not very useful, but you know.. just an interesting thing... _________________ ChucK => now; |
|
Back to top
|
|
|
Antimon
Joined: Jan 18, 2005 Posts: 4145 Location: Sweden
Audio files: 371
G2 patch files: 100
|
Posted: Sat Apr 02, 2011 6:09 am Post subject:
|
|
|
Yeah arrays work weird sometimes. I just take care to keep in mind that arrays are dodgy, and make sure I test my use of arrays thorougly before deciding that I can rely on my solutions.
In my world ChucK should have thrown a compilation error at your first assignment to a:
"Why are you trying to assign an integer value to an array? Stop doing that!"
/Stefan _________________ Antimon's Window
@soundcloud @Flattr home - you can't explain music |
|
Back to top
|
|
|
trzewiczek
Joined: Sep 24, 2010 Posts: 4 Location: Poland
|
Posted: Sat Apr 02, 2011 10:00 am Post subject:
|
|
|
With this:
Code: | int a[0];
a << 1;
a << 2;
|
it's quite ok for me. It' a shortcut for:
Code: | a.push( 1 );
a.push( 2 );
|
But then it comes to multidim arrays it's really weird!! You're pushing int where the array of ints should be pushed and no error is sent!
And btw I really don't like empty array declaration:
Code: | int a[0]; // zero length array?!
|
It looks ugly and for me is an example of a bad design style of ChucK.
There should be a special wiki pega with "Array's qeirdness"
Best,
trzewiczek _________________ ChucK => now; |
|
Back to top
|
|
|
|