the prototype of the function ctime is
char *ctime(const time_t *timep);
As we can see, it return a string. but, where the sting be contained?
and why we shouldn’t free the string’s memory
This is sample code will get a lots of error message
char *p;
p = ctime(...);
...
free(p);
*** glibc detected *** ./a.out: free(): invalid pointer: 0x00007f0b365b4e60 ***
It returns a pointer to a
staticbuffer, and must not befree()d. From man ctime:The C99 standard, section 7.23.3.2 The ctime function states that calling
ctime(timer)function is equivalent toasctime(localtime(timer)), and theasctime()implementation (as illustrated in the same document) is equivalent to:The argument passed to
free()must be a pointer returned by a call tomalloc(),calloc()orrealloc()only, otherwise the behaviour is undefined.