This code causes GCC to error out due to a bounds error with the following set -D_GLIBCXX_DEBUG -D_FORTIFY_SOURCE=2 The solution is to copy via iterator.