为什么时间(time_t的*)函数都返回并设置由-REF? [英] Why does time(time_t *) function both return and set the by-ref?

查看:115
本文介绍了为什么时间(time_t的*)函数都返回并设置由-REF?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直很好奇,为什么在时间(time_t的*)函数都返回一个 time_t的,和时间设置为指针传递的?

I've always been curious, why does the time(time_t *) function both return a time_t, and set the time to the passed in pointer?

返回时间的示例:

time_t myTime = time(NULL);
printf("The time is now %s", ctime(&myTime));

的值设置为指针的示例:

Example of setting the value to the pointer:

time_t myTime;
time(&myTime);
printf("The time is now %s", ctime(&myTime));

我原本以为会有被写入内存而不是返回的性能增益,但如果它做两件事,不只是让它慢?

I originally thought there would be a performance gain by writing to the memory instead of returning, but if it has to do both, doesn't that just make it slower?

推荐答案

有在它的当前定义的方式没有真正的好处。

There's no real benefit in the way it's currently defined.

我怀疑,当时间()最初定义的功能,它采用了无法从一个函数返回一个类型。非常早期的C实现没有长整型并没有能够从函数返回的结构。上与16位整数,重新present一个时间将是作为一个结构或作为阵列的唯一方式的系统;值得一秒钟16位是不到一天的时间。

I suspect that when the time() function was first defined, it used a type that could not be returned from a function. Very early C implementations didn't have long int and were not able to return structures from functions. On a system with 16-bit ints, the only way to represent a time would be as a structure or as an array; 16 bits worth of seconds is less than a day.

时间这么早实现()可能已经使用这样的事情(炒):

So early implementations of time() might have been used something like this (speculation):

time_t now;
time(&now);    /* sets now.time_high, now.time_low */

或者

int now[2];
time_t(now);    /* sets now[0], now[1] */

在后C实现增加更长的整数和值返回结构的能力,返回从时间 time_t的价值的能力()加入功能,但旧的功能保持,以避免破坏现有的code。

When later C implementations added longer integers and the ability to return structures by value, the ability to return a time_t value from the time() function was added, but the old functionality was kept to avoid breaking existing code.

我认为,如果时间()今天正在界定了,它会看起来更像是这样的:

I think that if time() were being defined today, it would look more like this:

time_t time(void);

我一直没能确认的时间旧的实现()函数的工作这种方式(尝试谷歌搜索时间!),但它给出有意义语言的历史。

I haven't been able to confirm that old implementations of the time() function worked this way (try Googling "time"!), but it makes sense given the history of the language.

如果你传递一个空指针到时间()函数,它返回当前时间也没有将其存储在一个变量;这就避免了一些性能损失的:

If you pass a null pointer to the time() function, it returns the current time without also storing it in a variable; this avoids some of the performance penalty:

time_t now = time(NULL);

这篇关于为什么时间(time_t的*)函数都返回并设置由-REF?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆