0

嘿,我正在使用 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;
}
4

1 回答 1

1

问题就在这里QueryPerformanceCounter((LARGE_INTEGER*)&time1);

unsigned long int将其地址传递给 时不要使用QueryPerformanceCounter

unsigned long int只保证至少 32 位。

使用 64 位变量

#include <cstdint>    // + include this
int64_t

或者

long long int
于 2013-03-20T17:25:33.527 回答