Your problem is that you're using an integer for something an unsigned integer should be doing. You should never have a negative size. Next, make sure your stuff is initialized before using them. Lastly, why are you using a reference to a pointer? Hopefully, your compiler is smart enough to realize what you're doing and will optimize it to save an extra indirection.