Question

I'm using ctime function to get a readable representation of time_t variable. ctime is declared as follows:

char *ctime (const time_t *timer);

and you can see that it returns a pointer to the resulting char array without any char pointer passed in arguments. Thus I'm wondering where does ctime allocate char buffer and who will destroy it and isn't allocating memory inside function (except various malloc's, of course) considered to be a mess.

Any suggestions?

Was it helpful?

Solution

From the link you quote:

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

That array lasts as long as the program does, it must not be freed by anyone.

POSIX 2008 has a ctime_r function that is re-entrant (you pass in your buffer), but it is not standard in C or C++.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top