I'm asking this question more out of curiosity than actual need, but this has been baffling me for a while. I'd really like to know what's wrong with my code below. BTW don't try to understand the aim of the function - it's just meant to show the problem.
The code below causes a segmentation fault when run on Linux (using gcc), but it works just fine on Windows (using Visual Studio). As far as I know, there's nothing wrong with returning a struct by value, so what am I doing wrong below?
#include <time.h>
#include <stdint.h>
using namespace std;
struct tm testFunc(const uint32_t rawtime) {
struct tm * localTime;
localTime = gmtime ((const time_t*)&rawtime);
struct tm testval = *localTime;
return testval;
}
int main() {
uint32_t now = 1538442104;
testFunc(now);
}
time_tis not auint32_t. This is undefined behavior. Read the manual pages.