You probably should use ALLOC_N
to manage memory allocation dynamically, where your C code needs to store things in independently of object data, and the stored data needs to persist over more than one method call (therefore you cannot be certain that deallocation within specific C functions would be called).
If you are just creating something then throwing it away, all within the context of one complex method call, then you can use plain C approaches for memory management. The C stack is fine for the real basics E.g. just declare int foo[1000];
and you can use that array internally, C will use the stack and clean up as normal - Ruby of course has no access to this data, unless you copy it into something at the end. Don't over-use this though, int foo[1000000];
risks blowing the stack and causing a segfault.
The documentation implies one other reason to use ALLOC_N
- you will avoid some out-of-memory situations due to the call to garbage collection. Also, you will get a slightly friendlier Ruby-managed process failure if you really are out of memory (as opposed to whatever your C routine would do).