Arc Forumnew | comments | leaders | submitlogin
2 points by stefano 6064 days ago | link | parent

Try:

  (cdef _wait "wait" cint (cptr))

  (def wait (i)
    (let pi (cmalloc 4)
      (cpset pi cint i)
      (_wait pi)))
This creates a pointer, assign it a value and returns the value returned by _wait.


1 point by eds 6059 days ago | link

Also, in your malloc() example, don't you have to explicitly free the memory afterwards? MzScheme's GC doesn't deal with memory explicitly allocated from C, does it? (I know the example itself uses only a tiny amount of memory, but still...)

-----

1 point by sacado 6059 days ago | link

Yes and no. I think mzscheme's GC can handle it without explicitly calling free at the right time, but you have to register a "finalizer" function to do so. That function will be called when the GC collects the object.

It does not know the size of malloced objects however, so be careful (no pb there, but if you allocate something very big, mzscheme will only see a new reference and will not necessarily call GC when you will actually lack memory).

-----

3 points by stefano 6058 days ago | link

If you alloc memory with cmalloc mzscheme will automatically delete it for you. Try

  (def infinite ()
    (cmalloc 4)
    (infinite))

  (infinite)
You shouldn't run out of memory. If you do, then I'm wrong.

-----

1 point by eds 6062 days ago | link

Just wondering: is there a way to malloc(sizeof(int)) from inside Arc instead of hard coding the size of an int?

-----

2 points by stefano 6061 days ago | link

You can make a C function like this:

  int size_of_int ()
  {
    return sizeof(int);
  }
and import it. I've followed this route while writing gtk.arc, but probably there's a better way to do it.

-----