添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接

gettimeofday()为什么会出现段错误

我两个程序,同样的程序流程,只不过一个是结构体变量,一个是结构体指针,但是为什么结构体指针运行完之后报错,说段错误。

正确的程序是这个:

#gettimeofday_r.c

C/C++ code#include

#include

#include

void foo()

int i = 0;

for (; i <= 10000; i++);

int main(int argc, char *argv[])

struct timeval start;

struct timeval end;

float timeuse;

gettimeofday(&start, NULL);

printf("start time is %d:%d\n", start.tv_sec, start.tv_usec);

foo();

gettimeofday(&end, NULL);

printf("end time is %d:%d\n", end.tv_sec, end.tv_usec);

timeuse = 1000000 * (end.tv_sec - start.tv_sec) + ( end.tv_usec - start.tv_usec );

printf("timeuse1 is %f \n", timeuse);

timeuse /= 1000000;

printf("timeuse2 is %f \n", timeuse);

printf("use time is %f\n", timeuse);

return 0;

运行结果是:

C/C++ codestart time is 1331190741:726493

end time is 1331190741:726691

timeuse1 is 198.000000

timeuse2 is 0.000198

use time is 0.000198

错误的代码是:

#gettimeofday_w.c

C/C++ code#include

#include

#include

void foo()

int i = 0;

for (; i <= 10000; i++);

int main(int argc, char *argv[])

struct timeval *start;

struct timeval *end;

float timeuse;

gettimeofday(start, NULL);

printf("start time is %d:%d\n", start->tv_sec, start->tv_usec);

foo();

gettimeofday(end, NULL);

printf("end time is %d:%d\n", end->tv_sec, end->tv_usec);

timeuse = 1000000 * (end->tv_sec - start->tv_sec) + ( end->tv_usec - start->tv_usec );

printf("timeuse1 is %f \n", timeuse);

timeuse /= 1000000;

printf("timeuse2 is %f \n", timeuse);

printf("use time is %f\n", timeuse);

return 0;

运行结果是:

C/C++ codestart time is 1331190767:111354

end time is 1331190767:111558

timeuse1 is 204.000000

timeuse2 is 0.000204

use time is 0.000204

Segmentation fault

错误的代码我用gdb调试,最后的程序到最后一个括号结束了,然后提示:

Cannot find bounds of current function

Program received signal SIGSEGV,segmentation fault

------解决方案--------------------

看一下函数声明就知道了,第一个参数必须是指针。

int gettimeofday(struct timeval *tv, struct timezone *tz);

------解决方案--------------------

你没有给指针分配空间啊

start=(struct timeval *)malloc(struct timeval);

gettimeofday(start, NULL);

------解决方案--------------------

函数需要一个指针,而你给了它一个未初始化的指针,程序没崩溃就算运气好了。

第一种才是标准用法,第二种需要这样:

struct timeval start;

struct timeval *pstart = &start;

gettimeofday(...);

Linux 下计算某个程序 执行的时间一般使用get time ofday 函数,此函数的声明在sys/ time .h文件中。此函数接收两个结构体参数,分别为 time val、 time zone.         两个结构体的声明如下: struct time val { time _t tv_sec; /* seconds */ long tv_usec; /* microseconds */ struct time zone { int tz_minuteswest; int tz_dst time ;         一般通过get time ofday 函数获得当前时间时,第二个 动手点关注干货不迷路????为什么我们需要一把分布式锁?为了效率(efficiency),协调各个客户端避免做重复的工作。即使锁 偶尔 失效了,只是可能把某些操作多做一遍而已,不 产生其它的不良后果。比如重复发送了一封同样的 email(当然这取决于业务应用的容忍度)。为了正确性(correctness)。在任何情况下都不允许锁失效的情况发生,因为一旦发生,就可能意味着数据不一致... 本文提供了cocos2d-x上获取毫秒级时间的方法,同样也提供了获取本地时间年月日时分秒的方法。 对于cocos2d-x高版本(比如2.1.3等某些版本)在win32平台下获取年月日时分秒的方法可能遇到的错误情况进行了分析,以及提供了解决办法。 本文介绍AliSQL的内核版本更新说明。MySQL 8.020200229新特性 Performance Agent:更加便捷的性能数据统计方案。通过MySQL插件的方式,实现MySQL实例内部各项性能数据的采集与统计。 在半同步模式下添加网络往返时间,并记录到性能数据。 性能优化 允许在只读实例上进行语句级并发控制(CCL)操作。 备实例支持Outline。 Proxy短连接优化。 优化不同CP...       最近遇到这样一 有bug的代码:#include&lt;stdio.h&gt; #include&lt;unistd.h&gt; #include &lt;stdint.h&gt; #include &lt;sys/ time .h&gt; uint64_t get_usec() struct time val now; get time ofday (&amp;now, NU... 场景get time ofday 函数是 Linux 系统下标准C函数,在Windows下使用 返回-1错误 Linux 调用方式:#include #include //添加头文件int64_t getCurrent Time () //直接调用这个函数就行了,返回值最好是int64_t,long long应该也可以{struct time val tv;get time ofday (&tv,NUL... void BThost_initEvent(BThost_Event* event,unsigned char manual_reset/*,const TCHAR* Eventname*/) event->is_set = FALSE; event->is_pulse= FALSE ; event->is_manual_reset= manual_reset; 最近在项目中使用到了get time ofday 这个函数来计算系统当前毫秒数,发现这里面有个问题需要特别注意一下。 1、首先来看一下函数原型:int get time ofday (struct time val *tv, struct time zone *tz) 其中结构体struct time val定义如下:struct time val { time _t tv_sec; /* s