python works, c++ version causes segfault