Few Questions

52 views
Skip to first unread message

Aaron

unread,
Jul 31, 2017, 12:52:31 PM7/31/17
to Extempore
Hello,

I'm fairly new to extempore, and am trying to get a hang of the scheme/xtlang combination. I have a few questions:

How do I allocate memory for an i8**? If I do some (let ((test:i8** (zalloc))) ... ) does this allocate memory for (pref test 0..n), or will it allocate memory for (pref (pref test 0..n) 0..m)? Do I need to use a memzone for this?

Strings literals are null terminated in the xtlang context correct? Is this a null, nil, '\0', or something else?

Thanks for any help! I love working with this language so far.

Best,

Aaron

Toby Gifford

unread,
Aug 1, 2017, 1:14:18 AM8/1/17
to extemp...@googlegroups.com
Hi Aaron, memory allocation works a little bit differently to either of the options you suggest. It took me a while to understand that the alloc functions are overloaded on return type (unlike C).  All of the alloc functions (zalloc, salloc, halloc) return a pointer of some type, and have as an optional argument an integer specifying how many things of that type to allocate (default is 1). So because you have told the compiler test is of type i8**, which the compiler thinks of as (i8*)*, it will allocate one i8* worth of memory, which at the moment means 64-bits (until a 32-bit version of extempore is released). If you want an array of cstrings (which I think is what you are trying to do), one way to achieve that would be as shown below. 

String literals in xtlang are null terminated yes. In this context null means 0:i8

;; Allocate all the memory for an array of cstrings
(bind-func create_array_of_cstrings:[i8**,i64,i64]*
  (lambda (num_strings:i64 max_string_length:i64)
    (let ((output:i8** (zalloc num_strings)))
      (doloop (i num_strings)
        (let ((str:i8* (zalloc (+ max_string_length 1))))
          (pset! output i str)))
      output)))


;; Put some values in
(bind-val my_cstring_array i8** (create_array_of_cstrings 4 255))
($ (pset! my_cstring_array 0 "Hello"))
($ (pset! my_cstring_array 1 "World"))


;; Check out the bit pattern that terminates the first string - output should be 00000000
($ (print_byte (i8toi64 (pref (pref my_cstring_array 0) 4))))




P. S. 
To run this last line you need a print_byte function, for example
(bind-func print_byte:[void,i64]*
  (lambda (d)
    (let* ((bits:|8,bool|* (alloc)))
      (doloop (n 8)
        (aset! bits n (not (= 0 (modulo d 2))))
        (set! d (/ d 2)))
      (doloop (m 8)
        (if (aref bits (- 7 m))
            (printf "%d" 1)
            (printf "%d" 0)))
      (printf "\n")
      void)))
The reason that I use an i64 input argument in this function rather than an i8 is that extempore's i8 conversion routines, for example i8toi64, are a bit unclear about whether the i8 is signed or unsigned. There's probably a better way to do it.






--
You received this message because you are subscribed to the Google Groups "Extempore" group.
To unsubscribe from this group and stop receiving emails from it, send an email to extemporelang+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Aaron

unread,
Aug 1, 2017, 11:46:53 AM8/1/17
to Extempore
Thank you so much! This has been very helpful
To unsubscribe from this group and stop receiving emails from it, send an email to extemporelan...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages