newLISP Fan Club

Forum => newLISP in the real world => Topic started by: pjot on May 07, 2005, 06:37:50 AM

Title: Let's eat CPU, memory, swap
Post by: pjot on May 07, 2005, 06:37:50 AM
Hi all,



By coincidence I found this interesting line:



(array 1 (address "newlisp"))


Don't try this at home kids.



Anyway you PC gets eaten. What happens?



Peter
Title:
Post by: Lutz on May 07, 2005, 07:37:43 AM
It creates an array with about 4 million cells. On my PC (address "newlisp") returns 4054840 which is the address of the string "newlisp" in memory, taken by the 'array' statement as a number. In my case it took about 2 seconds to create the array but then spends a lot of time displaying 4 miliion 'nil' and the Tcl/Tk text box or your command shell may eventually freeze on Windows (mostly be fine on Linux).



'address' is used when manipulating memory directly. It always returns the address of an item in memory, i.e. the address of a string or a number or float:



(set 'str "newLISP")
(set 'int-num 12345)
(set 'float-num 1.234)

(get-string (address str)) => "newLISP"
(get-int (address int-num)) = 12345
(get-float (address float-num)) = "1.234


You use all of the above frequently when writing interfaces to 'C' libraries. There is also a 'cpymem' which copies memory between two addresses.



As you say: "don't try this at home kids", it is only for grown up newLISP users and people not scared of segfaults, bus errors etc.,  ;)



Lutz
Title:
Post by: pjot on May 07, 2005, 09:50:19 AM
Well, for me it is perfectly OK that newLisp behaves like this! I made a mistake by trying to initialize an array with a couple of strings, as follows:



(array 3 "this" "is" "wrong")



But according to the docs, only lists can be used to initialize an array:



(array 3 '("this" "is" "right"))



So if I want to initialize an array with strings, it should go like this:



(set 't (array 3))

(nth-set 0 t "this")

(nth-set 1 t "is")

(nth-set 2 t "right")





Or can this be done faster?
Title:
Post by: Sammo on May 07, 2005, 10:19:08 AM
how about ...



> (define (array-init) (array (length (args)) (args)))

(lambda () (array (length (args)) (args)))



> (set 'a (array-init "one" "two" "three"))

("one" "two" "three")



> a

("one" "two" "three")



> (array? a)

true



or perhaps



> (define (array-init-with-dim dim) (array dim (args)))

(lambda (dim) (array dim (args)))



> (set 'b (array-init-with-dim 5 "one" "two" "three"))

("one" "two" "three" "one" "two")
Title:
Post by: pjot on May 07, 2005, 12:08:07 PM
OK so an immediate initialization for arrays needs it's own function.... no newLisp internal to accomplish it.



Thanks Sammo!