Int type fixes

I hit a few bugs around the integer type problems in the c++ wrapper code causing segfaults. I've fixed those and added extra checks for malloc in the reader.