In the documentation that describes the high-resolution timestamps, there is a question-and-answer section.
Under what circumstances does QueryPerformanceFrequency return FALSE, or QueryPerformanceCounter return zero?
This won’t occur on any system that runs Windows XP or later.
People on Stack Overflow have noted, “Nuh-uh! I can get it to fail even on Windows XP and later!”
The function can fail with error code
ERROR_NOACCESS(Invalid access to memory location) if the variable is not double-word (8-byte) aligned.
So who’s right?
The documentation assumes that you are passing valid parameters. Specifically, the pointer parameter is a valid pointer to a writable LARGE_INTEGER structure. And that means that it’s not a null pointer, it’s not a pointer to unallocated memory, it’s not a pointer to read-only memory, it’s not a pointer to memory that is freed while the function is executing, you don’t write to the memory while QueryÂPerformanceÂCounter is running, and it’s a pointer to properly aligned LARGE_INTEGER structure. (There are probably other ways the parameter can be invalid; those are just the ones I could think of off the top of my head.)
The LARGE_INTEGER structure as LONGLONG alignment, which on Windows means 8-byte alignment, because the default structure packing is /Zp8. Therefore, your output LARGE_INTEGER was not valid. (Indeed, in the example given, it’s not even a LARGE_INTEGER!)
If you pass invalid parameters, then you have broken the basic ground rules for programming, and the results will be unpredictable. The function might return failure, it might return success but produce garbage results, it might crash your process, it might merely corrupt your process in a way that causes it to crash 10 minutes later. Everything is on the table (as long as it doesn’t cross a security boundary).
The way they got it to fail was by passing invalid parameters. Clearly the function can’t succeed if you don’t give a valid place to put the answer.
But just to forestall the “Nuh uh”-ers, I made an update to the documentation for QueryÂPerformanceÂCounter:
On systems that run Windows XP or later, the function will always succeed when given valid parameters and will thus never return zero.
0 comments
Be the first to start the discussion.