嘿即时通讯使用QueryPerformanceCounter来计算该功能花费多长时间(以毫秒为单位),但是我遇到了此运行时错误:
Run-Time Check Failure #2 - Stack around variable 'time1' is corrupted.
香港专业教育学院搜索并尝试了一切,我不能弄清楚这个错误。有谁能够帮助我?
这是发生的代码:7
void print()
{
unsigned long int time1 = 0;
unsigned long int time2 = 0;
QueryPerformanceCounter((LARGE_INTEGER*)&time1);
//Loop through the elements in the array.
for(int index = 0; index < num_elements; index++)
{
//Print out the array index and the arrays elements.
cout <<"Index: " << index << "\tElement: " << m_array[index]<<endl;
}
//Prints out the number of elements and the size of the array.
cout<< "\nNumber of elements: " << num_elements;
cout<< "\nSize of the array: " << size << "\n";
QueryPerformanceCounter((LARGE_INTEGER*)&time2);
cout << "\nTime Taken : " << time1 - time2 <<endl;
}
最佳答案
问题在这里QueryPerformanceCounter((LARGE_INTEGER*)&time1);
将其地址传递给unsigned long int
时,请勿使用QueryPerformanceCounter
。unsigned long int
仅保证至少32位。
使用64位变量
#include <cstdint> // + include this
int64_t
要么
long long int
关于c++ - QueryPerformanceCounter运行时错误,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/15530507/