C function returns pointer to a struct
我已经开始学习C语言中的Linux编程,遇到了以下问题:
函数
我的问题是,初始化后如何管理
如果
那么返回指针的C函数的正确相位是什么呢?
谢谢!
根据本地时间的手册页(为了清晰起见,添加了粗体和斜体):
The localtime() function converts the calendar time timep to broken-down time representation, expressed relative to the user's specified timezone. The function acts as if it called tzset(3) and sets the external variables tzname with information about the current timezone, timezone with the difference between Coordinated Universal Time (UTC) and local standard time in seconds, and daylight to a nonzero value if daylight savings time rules apply during some part of the year. The return value points to a statically allocated struct which might be overwritten by subsequent calls to any of the date and time functions. The localtime_r() function does the same, but stores the data in a user-supplied struct. It need not set tzname, timezone, and daylight.
号
粗体部分表示返回值的行为与您猜测的完全相同,随后的调用可能覆盖先前返回的结构。
您要么需要立即缓存生成的结构,要么使用斜体部分中提到的函数。
凯文解释了问题所在,你的假设是正确的。对于此类函数,您可以应用一个简单的修复:
从
编辑:还有很多事情要说:
C99&C11提供更安全的功能[TR 24731-1]。
江户十一〔四〕号