World: r4wp
[#Red] Red language group
older newer | first last |
Henrik 11-Jul-2012 [631] | Leaving for the heavy metal festival. - It's good to see people celebrating parts of the periodic table of elements. Oh, maybe it means something else... :-) |
PeterWood 11-Jul-2012 [632] | Why the difference? Shouldn't W and X be same as V is same as P/VALUE? I checekd that v = p/value so it would appear to be the type casting that is the problem. I checekd the spec and casting a byte! to and integer! should be okay. So it seems like it is a bug. |
Arnold 11-Jul-2012 [633] | When will they put beer on the periodic table of elements? Hopefully after Red is finished? |
Rebolek 11-Jul-2012 [634] | f: func [ val [integer!] return: [struct! [value [integer!]]] /local s ][ s: declare struct! [value [integer!]] s/value: val s ] s1: f 1 print ["s1/value:" s1/value lf] s2: f 10 print ["s1/value:" s1/value lf] ; --- outputs: C:\code\Red\red-system\builds>test s1/value:1 s1/value:10 After setting S2 value, S1 is changed. Why? Is it a bug? |
Kaj 11-Jul-2012 [635] | Looks like it. Odd |
PeterWood 11-Jul-2012 [636x2] | I don't think it's a bug. s is a local (static) variable in function 'f. f returns a reference to s. So if you change the contants of s/value, all references to s will now refer to its new value. |
I added prints of s1 and s2 to your program; this is the output : s1 9348 s2 9348 | |
Rebolek 11-Jul-2012 [638] | So how I can return new struct! each time? |
PeterWood 11-Jul-2012 [639x3] | Only by declaring the struct! in your main program and passing it (its reference) as an arugment: f: func [ val [integer!] s [struct! [value [integer!]]] ][ s/value: val ] |
Well that's the only way that I've found so far. | |
Once the Red memory manager is available it may well be possible to use it to allocate struct! in a function but then there is the problem of how to release the memory later. From a memory mangement point of view, it seems easiest not to allocate variables inside functions to be returned to the calling program/function. | |
Rebolek 11-Jul-2012 [642] | ok, thanks |
Kaj 11-Jul-2012 [643x2] | Oh right, the pointer is returned from the function, not the value |
There's no problem in allocating memory to return from a function, but you have to use ALLOCATE and later FREE it | |
Rebolek 11-Jul-2012 [645] | Ok, so this version does what I expected :) f: func [ val [integer!] return: [struct! [value [integer!]]] /local s ][ s: declare struct! [value [integer!]] t: allocate size? s s/value: val t: copy-memory t as byte-ptr! s size? s as struct! [value [integer!]] t ] s1: f 1 print ["s1/value:" s1/value lf] ; == 1 s2: f 10 print ["s1/value:" s1/value lf] ; == 1 |
Kaj 11-Jul-2012 [646x5] | You don't have to declare an intermediate struct and copy it |
t: as struct! [value [integer!]] allocate size? s | |
t/value: val | |
t | |
You should check the allocation for being a null pointer, though | |
Rebolek 11-Jul-2012 [651] | how that can happen? |
Kaj 11-Jul-2012 [652x3] | If you don't get the memory from the operating system |
The form-* functions in the C library binding do pretty much that: | |
http://red.esperconsultancy.nl/Red-C-library/artifact/a1a55e070454c421657185b1d9d09f480d6c5587 | |
Rebolek 12-Jul-2012 [655] | Another question :) Is there any easy way to convert integer! to float! ? I wrote something for float! to integer!, but with integer! to float! I'm bit lost. |
Ladislav 12-Jul-2012 [656x3] | Another question :) Is there any easy way to convert integer! to float! ? I wrote something for float! to integer!, but with integer! to float! I'm bit lost. - see http://www.fm.tul.cz/~ladislav/rebol/library-utils.r |
The approach is as follows: 1) convert integer to double (=decimal!) 2) convert double to float | |
aha, sorry, this is a Red group... | |
Rebolek 12-Jul-2012 [659x2] | yes, I'm looking for Red/System solution. |
Why does this code throw *** Runtime Error 11: float stack check ? i: 0 f: 0.0 p: declare pointer! [float!] arr: allocate 100 while [i < 10][ p: as pointer! [float!] arr + (i * 8) p/value: f f: f + 0.1 i: i + 1 ] | |
Kaj 12-Jul-2012 [661x5] | Sounds like a bug, although the program is overly complex |
i: 0 f: 0.0 arr: as pointer! [float!] allocate 10 * size? float! while [i < 10][ p/i/value: f f: f + 0.1 i: i + 1 ] | |
That should work, and be more secure | |
arr/i/value: f | |
Oh, with path notation, you need to adjust the loop to start counting the index from 1 | |
Rebolek 12-Jul-2012 [666] | I'm still discovering how Red/System works :) |
Kaj 12-Jul-2012 [667] | If your original program keeps erroring out, please enter it in the bug tracker |
Rebolek 13-Jul-2012 [668] | Kaj, your code throws compilation error invalid struct member i in: arr/i/value |
Kaj 13-Jul-2012 [669x4] | Ah, it's actually even simpler :-) |
i: 1 f: 0.0 arr: as pointer! [float!] allocate 10 * size? float! while [i <= 10] [ arr/i: f f: f + 0.1 i: i + 1 ] | |
In Linux, on your original code, I get: | |
*** Runtime Error 9: float invalid operation *** at: 08048397h | |
Rebolek 13-Jul-2012 [673] | With your latest version I get *** Runtime Error 11: float stack check (on windows). |
Kaj 13-Jul-2012 [674] | Better enter both in the bug tracker, then |
DocKimbel 13-Jul-2012 [675] | Rebolek: yes, add them to the bugtracker, I will fix them this weekend. |
Rebolek 13-Jul-2012 [676x2] | That's great, thanks! |
Just an update, I just tried latest Kaj's code on Ubuntu under VirtualBox and I get *** Runtime Error 9: float invalid operation | |
DocKimbel 13-Jul-2012 [678] | It looks like we haven't tested floats pointer arithmetic enough. |
Kaj 13-Jul-2012 [679] | Is that 64 bit Ubuntu? |
Rebolek 13-Jul-2012 [680] | 32bit Lubuntu |
older newer | first last |