• Advertisement
Sign in to follow this  

who allocates the memory (asctime)?

This topic is 4251 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

own code made from sample code with only minor changes:
char *timestr;
struct tm *newtime;
time_t aclock;
time(&aclock);
newtime = localtime(&aclock);
timestr = asctime(newtime);
does asctime allocate memory to the timestr pointer that I have to release? Or what exactly does the timestr pointer point towards? AFAIK there's no char* entry in the tm struct, so the memory must come from somewhere else, or?! The question is - if I use the code above will I get a memory leak and do I have to release some memory that isn't released above?

Share this post


Link to post
Share on other sites
Advertisement
It is most likely that asctime simply returns the address of an internal buffer. This buffer is either not allocated dynamically (most probably this is the case, since it's fixed size anyway) or will eventually be freed by the CRT during program exit. The hint on MSDN supports this assumption:
Quote:

Each call to this function destroys the result of the previous call.


HTH,
Pat.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement