在time.h中用一个常用结构:struct tm {
        int tm_sec;     /* seconds after the minute - [0,59] */
        int tm_min;     /* minutes after the hour - [0,59] */
        int tm_hour;    /* hours since midnight - [0,23] */
        int tm_mday;    /* day of the month - [1,31] */
        int tm_mon;     /* months since January - [0,11] */
        int tm_year;    /* years since 1900 */
        int tm_wday;    /* days since Sunday - [0,6] */
        int tm_yday;    /* days since January 1 - [0,365] */
        int tm_isdst;   /* daylight savings time flag */
        };    我们一般如下使用:
   struct tm *newtime=NULL;
time_t  long_time;
time(&long_time);
newtime=localtime(&long_time);//函数返回一个结构(即一段内存)
//localtime 中内存如何管理的 如果是malloc一段内存,为何我们没有显式的free而不发生内存泄漏?

解决方案 »

  1.   

    系统自己会释放掉自己分配的内存,如果是你malloc的,而没有FREE,就会发生内存泄露,比如把你的代码改成:
             struct tm *newtime=NULL;
    time_t*  long_time=new time_t;
    time(long_time);
    newtime=localtime(long_time);
    你不DELETE,看看会不会内存泄露。
                           
      

  2.   

    楼上的approach,ColderRain兄:
       
    struct tm *newtime=NULL;
    .....
    newtime=localtime(&long_time);
    ....
    在这里,localtime函数向系统申请了sizeof(tm)字节的内存,但是我们的程序并没有释放这段内存,依ColderRain的意思,是系统在程序退出时自己释放的,难道标准C也有gc功能 ?