| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 3332 人关注过本帖, 1 人收藏
标题:ctime 函数实现疑惑
只看楼主 加入收藏
vfdff
Rank: 6Rank: 6
等 级:侠之大者
威 望:8
帖 子:2172
专家分:425
注 册:2005-7-15
结帖率:79.17%
收藏(1)
已结贴  问题点数:10 回复次数:6 
ctime 函数实现疑惑
我们可以看到c函数库手册中其原型char *ctime(const time_t *time);
显然输入参数和函数返回值类型不一致,因此,这样函数返回值的字符串就不可能由输入参数指定这个字符串的空间地址

这样的话,我就有点疑问了,这个ctime函数返回值的字符串指针到底是一个全局变量呢?还只是一个静态变量呢?
显然这个字符串空间不会由malloc函数获取,否则空间无法释放会造成内存泄露

搜索更多相关主题的帖子: ctime 函数 
2010-07-04 04:02
vfdff
Rank: 6Rank: 6
等 级:侠之大者
威 望:8
帖 子:2172
专家分:425
注 册:2005-7-15
收藏
得分:0 
程序代码:
#include <stdio.h>
#include <time.h>

int main(void)
{
    time_t t;
  
    time(&t);
    printf("Today's date and time: %s\n", ctime(&t));
    printf("The string address 1 : %x\n", ctime(&t));
    printf("The string address 2 : %x\n", ctime(&t));
    return 0;
}
上述代码执行结果显示这个地址值一致不变,更加确认这个字符串地址值不是malloc动态申请的

图片附件: 游客没有浏览图片的权限,请 登录注册

~~~~~~~~~~~~~~~好好学习~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2010-07-04 04:05
vfdff
Rank: 6Rank: 6
等 级:侠之大者
威 望:8
帖 子:2172
专家分:425
注 册:2005-7-15
收藏
得分:0 
图片附件: 游客没有浏览图片的权限,请 登录注册

在SUSE11.2的GCC对这个ctime函数反汇编可以看到这个函数调用了asctime函数,然后继续对这个asctime函数反汇编,可以看到在jmp 0xb7ef2a40前使用
movl $0x72,0x4(%esp)把一个常量赋值给asctime函数内的一个局部变量(0x4(%esp))? 接下来不知道什么意思了

~~~~~~~~~~~~~~~好好学习~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2010-07-04 04:40
BlueGuy
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:29
帖 子:4476
专家分:4055
注 册:2009-4-18
收藏
得分:10 
ctime function

char * ctime ( const time_t * timer ); <ctime>

Convert time_t value to string

Converts the time_t object pointed by timer to a C string containing a human-readable version of the corresponding local time and date.

The returned string has the following format:

Www Mmm dd hh:mm:ss yyyy

Where Www is the weekday, Mmm the month in letters, dd the day of the month, hh:mm:ss the time, and yyyy the year.

The string is followed by a new-line character ('\n') and the terminating null-character.

This function is equivalent to: asctime(localtime(timer)).


Parameters
timer
Pointer to a time_t object that contains a calendar time.

Return Value
A C string containing the date and time information in a human-readable format.
The array which holds this string is statically allocated and shared by both the ctime and asctime functions. Each time either one of these functions is called the content of this array is overwritten.


我就是真命天子,顺我者生,逆我者死!
2010-07-04 09:58
lijm1989
Rank: 11Rank: 11Rank: 11Rank: 11
来 自:珠海
等 级:贵宾
威 望:12
帖 子:675
专家分:2844
注 册:2009-10-14
收藏
得分:0 
程序代码:
/* 这里的函数在GNU libc的locale/C-time.c中定义  */
#include <stdio.h>
#include <time.h>

extern const struct locale_data _nl_C_LC_TIME attribute_hidden;

#define ab_day_name(DAY) (_nl_C_LC_TIME.values[_NL_ITEM_INDEX (ABDAY_1)+(DAY)].string)
#define ab_month_name(MON) (_nl_C_LC_TIME.values[_NL_ITEM_INDEX (ABMON_1)+(MON)].string)

static const char format[] = "%.3s %.3s%3d %.2d:%.2d:%.2d %d\n";
static char result[ 3+1+3+1+20+1+20+1+20+1+20+1+20+1+1];

static char * asctime_internal (const struct tm *tp, char *buf, size_t buflen)
{
  if (tp == NULL)
    {
      __set_errno (EINVAL);
      return NULL;
    }
  if (__builtin_expect (tp->tm_year > INT_MAX - 1900, 0))
    {
    eoverflow:
      __set_errno (EOVERFLOW);
      return NULL;
    }
  int n = __snprintf (buf, buflen, format,
              (tp->tm_wday < 0 || tp->tm_wday >= 7 ?
               "???" : ab_day_name (tp->tm_wday)),
              (tp->tm_mon < 0 || tp->tm_mon >= 12 ?
               "???" : ab_month_name (tp->tm_mon)),
              tp->tm_mday, tp->tm_hour, tp->tm_min,
              tp->tm_sec, 1900 + tp->tm_year);
  if (n < 0)
    return NULL;
  if (n >= buflen)
    goto eoverflow;
  return buf;
}

char * __asctime_r (const struct tm *tp, char *buf)
{
  return asctime_internal (tp, buf, 26);
}

weak_alias (__asctime_r, asctime_r)

char * asctime (const struct tm *tp)
{
  return asctime_internal (tp, result, sizeof (result));
}

libc_hidden_def (asctime)

char * ctime (const time_t *t)
{
  return asctime (localtime (t));
}
2010-07-04 13:09
vfdff
Rank: 6Rank: 6
等 级:侠之大者
威 望:8
帖 子:2172
专家分:425
注 册:2005-7-15
收藏
得分:0 
以下是引用BlueGuy在2010-7-4 09:58:45的发言:

ctime function

char * ctime ( const time_t * timer );  

Convert time_t value to string

Converts the time_t object pointed by timer to a C string containing a human-readable version of the corresponding local time and date.

The returned string has the following format:

Www Mmm dd hh:mm:ss yyyy

Where Www is the weekday, Mmm the month in letters, dd the day of the month, hh:mm:ss the time, and yyyy the year.

The string is followed by a new-line character ('\n') and the terminating null-character.

This function is equivalent to: asctime(localtime(timer)).


Parameters
timer
Pointer to a time_t object that contains a calendar time.

Return Value
A C string containing the date and time information in a human-readable format.
The array which holds this string is statically allocated and shared by both the ctime and asctime functions. Each time either one of these functions is called the content of this array is overwritten.
您好,请问您这个是哪里查到的呢 ?
我的2005的MSDN上怎么没有呢?您使用哪个版本的

~~~~~~~~~~~~~~~好好学习~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2010-07-04 16:59
vfdff
Rank: 6Rank: 6
等 级:侠之大者
威 望:8
帖 子:2172
专家分:425
注 册:2005-7-15
收藏
得分:0 
知道了,原来这里可以查到详细的说明
http://www.

~~~~~~~~~~~~~~~好好学习~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2010-07-04 17:21
快速回复:ctime 函数实现疑惑
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.026943 second(s), 10 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved