Lots of malloc/free going on here. Consider using std::string instead.