1. There is a serious bug in your first snippet. The index has to be ty, not j!

2. That snippet is perfect.

3. sys_malloc and sys_free are the engine versions of malloc and free. They got several advantages over the original c versions. For example the engine will warn you in the log file if there is memory that was allocated with sys_malloc but has not been freed on engine shutdown. You can also mark memory allocated this way with the sys_mark function. The engine will warn you when you accidentally overwrite such marked memory areas.

Last but not least I guess(!) that sys_malloc may be faster than malloc. A call to malloc will definitely ask the operating system for memory while a call to sys_malloc may very well return memory that has been already allocated by the engine and hence is immediately available.

Also do not underestimate the feature that memory returned by sys_malloc is completely cleared. This can really save your arse, when you forgot to initialize memory.


Always learn from history, to be sure you make the same mistakes again...