Consider making m_keybuf an std::vector<char>. That way you don't have to do error-prone mucking about with memory and casting of malloc/realloc.