don't check for heaps_array != NULL in the loop. The check is done after the loop since it is allowed for the allocation to fail until finding aa ssuitable heap.