If a reallocation happens, the storage is allocated using the container's allocator, which may throw exceptions on failure (for the default allocator, bad_alloc is thrown if the allocation request does not succeed).

This seems to be my problem, but I don't understand why the allocation request wouldn't succeed.

I don't have revised your code in depth yet, but consider this: Avid raw-pointers and use references/smart_ptrs, and you will have less (or none) memory-management headaches.
–
Manu343726Aug 10 '13 at 12:22

return &token; is returning the address of a local var in your getToken(), which is undefined behavior. Just return a Token and call it good. There is no need to access a token by-address in this code at all.
–
WhozCraigAug 10 '13 at 12:22

1 Answer
1

The problem is that you in getNext you return a pointer to a local variable. Remember that when the function returns all local variables are destructed and so the pointer now points to a destructed object. Or, since local variables are on the stack and the stack is reused between function calls, it can now point to something completely different. The result of you then dereferencing this now invalid pointer leads to undefined behavior, and it's common for undefined behavior to cause crashes, but it may also seem to work but the data is completely screwed up.

The obvious solution is of course to return a copy of the object, i.e. don't use pointers (which is a good tip in general).