QueryPerformanceCounter运行时错误

时间:2013-03-20 17:23:35

标签: c++ performancecounter

嘿我使用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;
}

1 个答案:

答案 0 :(得分:1)

问题出在QueryPerformanceCounter((LARGE_INTEGER*)&time1);

将地址传递给unsigned long int时,请勿使用QueryPerformanceCounter

unsigned long int仅保证至少32位。

使用64位变量

#include <cstdint>    // + include this
int64_t

long long int