QueryPerformanceCounter运行时错误

QueryPerformanceCounter run-time error

本文关键字:运行时错误 QueryPerformanceCounter      更新时间:2023-10-16

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

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

CCD_ 4仅保证至少32位。

使用64位可变

#include <cstdint>    // + include this
int64_t

long long int