Add explicit casts to malloc() calls for C++ compatibility