working cpp pointer but weird runtime behavior