Code optimization: avoid lot of calls for alloc/free memory in cycle.