times(NULL) Segmentation fault

本文探讨了在嵌入式Linux环境下使用times(NULL)可能触发的段错误问题,并提供了修改后的代码示例来规避此问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

******************************************************************************
 *                   times(NULL) Segmentation fault
 * 说明:
 *     在PC机上times(NULL)是没问题的,但在嵌入式Linux上,貌似不传递参数是有可能
 * 会出问题的。
 *
 *                                         2017-7-6 深圳 龙华樟坑村 曾剑锋
 *****************************************************************************/

一、参考文档:
    1. Segmentation fault using ctime
        https://stackoverflow.com/questions/12338947/segmentation-fault-using-ctime    
    2. Segmentation fault on time(0);
        https://stackoverflow.com/questions/1731802/segmentation-fault-on-time0
    3. segmentation fault (core dumped) error when using time function
        https://stackoverflow.com/questions/20939415/segmentation-fault-core-dumped-error-when-using-time-function
   4. An application encounters segmentation fault in glibc's times(2) function
     https://access.redhat.com/solutions/1124633
二、测试代码: #include <iostream> #include <sys/times.h> #include <unistd.h> using namespace std; int main() { unsigned long SysF = sysconf(_SC_CLK_TCK); struct tms tmp; while(1) { // unsigned long tick = times(NULL); // don't use this. it may cause segmentation fault. unsigned long tick = times(&tmp); unsigned long RetUl = (1000 / SysF) * tick; cout << "tick: " << tick << " RetUl: " << RetUl << endl; usleep(10000); } return 0; }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值