gpt4 book ai didi

C++ linux时间卡住应用程序

转载 作者:太空狗 更新时间:2023-10-29 23:34:00 28 4
gpt4 key购买 nike

我正在导入 <sys/time.h>并使用

struct time_t *start;

一个类有一个成员time_t *start;和一个成员函数 getETA() :

template <class S, class P> 
double Task<S,P>::getETA()
{
time_t *stop;
time(stop);
return difftime(*stop , *start);
}

另请注意,time_t *start 是在类构造函数中初始化的。

每当我调用 getETA() 时,应用程序就会卡住。更有趣的是,我在主循环的 main.cpp 中使用了相同的结构和代码,显然它也卡住了应用程序:

int main(int argc, char **argv) 
{
...
time_t *start;
time(start);
...
}

我是不是用错了?我记得很久以前在某个应用程序中,我就是这样使用它的,它运行良好。

最佳答案

您必须将有效指针传递给 time() 函数。那你为什么不这样做:

time_t start;
time(&start);

time_t stop;
time(&stop);

也就是说,不要将startstop 声明为指针。将它们声明为非指针自动变量。

如果您将它们声明为指针,那么您必须这样做:

time_t *start = new time_t;
time(start);

time_t *stop = new time_t;
time(stop);

//...

//Must deallocate the memory when you don't need them anymore
delete start;
delete stop;

但我不建议这样做。我建议您使用非指针变量。

关于C++ linux时间卡住应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6885583/

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