Using malloc instead of new, treating 1d array as a 2d array to reduce allocations and improve performance.