I need to return a unsigned int* from a function. The code below will compile but will crash at run time on a Windows 64 bit machine. I know I am making a silly mistake somewhere and can someone point it out for me. :p. I also have declared the function in my header, so I know its not that error.
Please note I have censored the variable names and numbers because the problem in which this function resides is not for public release yet.
Function:
unsigned int* convertTime(unsigned int inputInteger, unsigned short inputFrac) {
unsigned int* output = new unsigned int[2];
double messageTimeFraction = double(inputFrac) * 20e-6;
output[1] = unsigned int(inputInteger + 2209032000);
output[2] = unsigned int(messageTimeFraction * 2e32);
return output; // Seconds
}
Implementation:
unsigned int* timeStamp;
timeStamp = convertTime(inputInteger,inputFrac);
unsigned int(...)cast isn't strictly valid (Visual C++ supports it as an extension). There's a related, somewhat technical question about this. You can just useunsigned(...)or you can use(unsigned int)(...).intis 32 bits in Visual C++ compiling for a 64-bit app, sounsigned int(inputFrac * 2e32)is going to be meaningless, and almost certainly uniformly 0. This shouldn't cause a crash, but it may be an error anyway. In addition, 2209032000 is a meaningless magic number, andmessageTimeFractionis unused. If this isn't the complete function, please let us know.