C++ implementation of mem