Convert memory.c to C++