Let's eat CPU, memory, swap

Q&A's, tips, howto's
Locked
pjot
Posts: 733
Joined: Thu Feb 26, 2004 10:19 pm
Location: The Hague, The Netherlands
Contact:

Let's eat CPU, memory, swap

Post by pjot »

Hi all,

By coincidence I found this interesting line:

Code: Select all

(array 1 (address "newlisp"))
Don't try this at home kids.

Anyway you PC gets eaten. What happens?

Peter

Lutz
Posts: 5289
Joined: Thu Sep 26, 2002 4:45 pm
Location: Pasadena, California
Contact:

Post by Lutz »

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:

Code: Select all

(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

pjot
Posts: 733
Joined: Thu Feb 26, 2004 10:19 pm
Location: The Hague, The Netherlands
Contact:

Post by pjot »

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?

Sammo
Posts: 180
Joined: Sat Dec 06, 2003 6:11 pm
Location: Loveland, Colorado USA

Post by Sammo »

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")

pjot
Posts: 733
Joined: Thu Feb 26, 2004 10:19 pm
Location: The Hague, The Netherlands
Contact:

Post by pjot »

OK so an immediate initialization for arrays needs it's own function.... no newLisp internal to accomplish it.

Thanks Sammo!

Locked