gpt4 book ai didi

c++ - QueryPerformanceCounter 运行时错误

转载 作者:行者123 更新时间:2023-11-28 07:42:23 25 4
gpt4 key购买 nike

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

unsigned long int 只保证至少 32 位。

使用 64 位变量

#include <cstdint>    // + include this
int64_t

long long int

关于c++ - QueryPerformanceCounter 运行时错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15530507/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com