This should be done with SGVector, not with MALLOC/FREE.
Also SGVector has a method ::zero() which again makes code much more easy to read and shorter