dynamic array crashes for "int ar[]" but not "int ar[0]"
Hi, array.size(int) and the '<<' operator are crashing miniAudicle (version 0.2.0, on Windows 7 pro). (functionality added to chuck in 1.2.1.2, http://chuck.cs.princeton.edu/release/VERSIONS ). miniAudicle tells me it's running chuck 1.2.1.2. the following code produces a "NullPointerException" error int ar[]; ar.size(10); // crashes here, "NullPointerException: shred[id=1:*unnamed3], PC=[5]" for (0 => int i; i < 4; i++) { ar << i; // crashes here, "NullPointerException: (array append) in shred[id=1:*unnamed3], PC=[13]" } playing around a bit, I now find that if I follow what the example array_dynamic.ck says and replace "int ar[]" with "int ar[0]", then the code works. I guess that means this isn't a bug but a meaningful difference between ar[] and ar[0]? In any case, I might amend the array_dynamic.ck to say something like "float argh[0]; // must set argh[0] and not argh[]" Thanks again, George On Fri, Jul 27, 2012 at 12:44 PM, George Locke < george.locke.maxmsp@gmail.com> wrote:
thanks!
On Fri, Jul 27, 2012 at 10:31 AM, Julien Saint-Martin < julien.saintmartin@googlemail.com> wrote:
Hi George,
You can check examples in Chuck installation directory: \examples\array\ array_resize.ck and array_dynamic.ck Personally I use: array.size(new_size); or array << new_element; // to add an element.
Happy chucking, Julien
2012/7/27 George Locke
Hi,
I am creating an array, but i don't know how big I need it to be at the time it is instantiated. Once I figure out what I want that size to be, how do I assign it? The current solution i have is this:
int array[]; int size; // do stuff { // new block keeps dummy from clogging namespace int dummy[size]; dummy @=> array; }
Is there a better way?
Regards, George
_______________________________________________ chuck-users mailing list chuck-users@lists.cs.princeton.edu https://lists.cs.princeton.edu/mailman/listinfo/chuck-users
_______________________________________________ chuck-users mailing list chuck-users@lists.cs.princeton.edu https://lists.cs.princeton.edu/mailman/listinfo/chuck-users
Hi,
Hey George!
array.size(int) and the '<<' operator are crashing miniAudicle (version 0.2.0, on Windows 7 pro). (functionality added to chuck in 1.2.1.2, http://chuck.cs.princeton.edu/release/VERSIONS ). miniAudicle tells me it's running chuck 1.2.1.2.
Check.
playing around a bit, I now find that if I follow what the example array_dynamic.ck says and replace "int ar[]" with "int ar[0]", then the code works. I guess that means this isn't a bug but a meaningful difference between ar[] and ar[0]? In any case, I might amend the array_dynamic.ck to say something like "float argh[0]; // must set argh[0] and not argh[]"
Well, if ChucK crashes it's a bug; ChucK should not crash if you make a syntax error, it should catch it at parsing and inform you. It is also not unambiguously clear to me that what you tried is actually a error, IMHO this should be fair game, it looks natural and expressive. I can tell you what goes wrong though. "int ar[]" creates a reference to a array but does not instantiate it. You'd use it in a context like this; [1, 2, 3] @=> int ar[]; Evidently ar.size(int a) should instantiate the new array entries if a is larger than the old size, which it'll always be for uninstantiated arrays so I find what you tried defensible, but ChucK is also a bit right to complain about a null-pointer as the reference doesn't point to anything yet at that point. Syntactically it seems a bit awkward, compared to the rest of ChucK where we don't need to care about memory management, though it might also have uses where this kind of explicit control is useful. I hope that clarifies things a bit? If all of this sounded like abracadabra; just use the one that works, that's what this comes down to :-) Yours, Kas.
Hey Kassen,
Thanks for looking into it. I'm picking up what you're putting down,
pretty much. int ar[] is a reference to nothing, like a null pointer, so
asking the VM to call a method is accessing uninitialized memory, like a
seg fault. I think I shouldn't have said "crashes" when what I meant was,
the thread dies and throws an exception, not that the audicle executable
nor the VM breaks. It's a run-time error and not a syntax error; that's
what I had in mind.
I am glad to understand ChucK a little better.
Thanks again,
George
On Sat, Jul 28, 2012 at 9:42 AM, Kassen
Hi,
Hey George!
array.size(int) and the '<<' operator are crashing miniAudicle (version 0.2.0, on Windows 7 pro). (functionality added to chuck in 1.2.1.2, http://chuck.cs.princeton.edu/release/VERSIONS ). miniAudicle tells me
it's
running chuck 1.2.1.2.
Check.
playing around a bit, I now find that if I follow what the example array_dynamic.ck says and replace "int ar[]" with "int ar[0]", then the code works. I guess that means this isn't a bug but a meaningful difference between ar[] and ar[0]? In any case, I might amend the array_dynamic.ckto say something like "float argh[0]; // must set argh[0] and not argh[]"
Well, if ChucK crashes it's a bug; ChucK should not crash if you make a syntax error, it should catch it at parsing and inform you. It is also not unambiguously clear to me that what you tried is actually a error, IMHO this should be fair game, it looks natural and expressive. I can tell you what goes wrong though.
"int ar[]" creates a reference to a array but does not instantiate it. You'd use it in a context like this; [1, 2, 3] @=> int ar[];
Evidently ar.size(int a) should instantiate the new array entries if a is larger than the old size, which it'll always be for uninstantiated arrays so I find what you tried defensible, but ChucK is also a bit right to complain about a null-pointer as the reference doesn't point to anything yet at that point. Syntactically it seems a bit awkward, compared to the rest of ChucK where we don't need to care about memory management, though it might also have uses where this kind of explicit control is useful.
I hope that clarifies things a bit? If all of this sounded like abracadabra; just use the one that works, that's what this comes down to :-)
Yours, Kas. _______________________________________________ chuck-users mailing list chuck-users@lists.cs.princeton.edu https://lists.cs.princeton.edu/mailman/listinfo/chuck-users
participants (2)
-
George Locke
-
Kassen