[Solved] Error with a references C++


The localtime function is not thread-safe and more importantly not reentrant.

The pointer it return is most likely a pointer to an internal static buffer. This means that each localtime call returns the very same pointer to the very same “buffer” (structure). In fact, if you read the linked reference, the buffer (structure) can be shared between multiple functions.

This can easily be checked by using a debugger and compare the pointers returned by the function.

If you need different values, then you need to copy the data instead of copy the pointer. This is simply done by making now and step structure instances instead of pointers. Then dereference the pointer returned by localtime:

struct tm now, step;  // Note: Not pointers!

...

now = *localtime(&t);  // Dereference returned pointer

1

solved Error with a references C++