Hello,

I'm having problems with memory allocation too. How does this actually work:
If I reserve memory with e.g. ALLOC_N, do I need to register it to gc 
somehow? If I put the memory to a C variable, say:

     int a = ALLOC_N(int, 2);

Does this memory get freed by gc or not and if yes, when? If it doesn't, 
I guess I must use 'free' and then what is the point of using ALLOC_N?
I don't want/need to put it into a Ruby object.

Also, if I use plain malloc, is it possible that gc frees it?

It seems that ALLOCA_N uses alloca which is not recommended? And is it 
so that alloca might fail for very large requests where malloc works?

Could someone give an example of correct memory allocation? I could not 
really get the idea by reading the sources. E.g. in the post below, I 
don't see where the registration to gc might occur. Documentation on 
this is badly needed.
Thanks.
m.


Mauricio FernŠŌdez wrote:
> #define REALLOC_N(var,type,n) (var)=(type*)xrealloc((char*)(var),sizeof(type)*(n))
> #define xrealloc ruby_xrealloc
> 
> void *
> ruby_xrealloc(ptr, size)
>     void *ptr;
>     long size;
> {
>     void *mem;
> 
>     if (size < 0) {
>         rb_raise(rb_eArgError, "negative re-allocation size");
>     }
>     if (!ptr) return xmalloc(size);
>     if (size == 0) size = 1;
>     malloc_increase += size;
>     RUBY_CRITICAL(mem = realloc(ptr, size));
>     if (!mem) {
>         rb_gc();
>         RUBY_CRITICAL(mem = realloc(ptr, size));
>         if (!mem) {
>             rb_memerror();
>         }
>     }
> 
>     return mem;
> }
> 
> REALLOC_N is passed a pointer, the corresponding type and the number
> of objects to (re)allocate place for. (Re)allocation is tried first and
> in case of failure a GC run is launched. If it fails twice an exception
> (NoMemError) is raised.
> 
> In case of success, newly allocated memory (past the block that was
> given the REALLOC_N) is uninitialized.