Linux programming problem

In the recently wrote in a time related procedures, as follows:
/*
 * Get the local time
 * Returns the string format "_ _ day month year"
 */
char *get_local_time_y_m_d()
{
	time_t *the_time;
	struct tm tm_ymd;
	char *buf;
	the_time=(time_t *)malloc(sizeof(time_t));
	//tm_ymd=(struct tm *)malloc(sizeof(struct tm));
	buf=(char *)malloc(sizeof(char)*16);
	memset(buf,'\0',sizeof(char)*16);

	//printf("%p\n%p\n",the_time,tm_ptr);

	time(the_time);
	//tm_ymd=gmtime(the_time);
	localtime_r(the_time,&tm_ymd);
	//memcpy(tm_ymd,localtime(the_time),sizeof(struct tm));
	strftime(buf,16,"%Y_%m_%d",&tm_ymd);
	free(the_time);
	/*
	 * Looks like struct TM structure can't seem to free
	 */
	//free(tm_ptr);
	return buf;
}
/*
 * Get the local time
 * Returns the string format "_ divided _ seconds"
 */
char *get_local_time_h_m_s()
{
	time_t *the_time;
	struct tm tm_hms;
	char *buf;
	the_time=(time_t *)malloc(sizeof(time_t));
	//tm_hms=(struct tm *)malloc(sizeof(struct tm));
	buf=(char *)malloc(sizeof(char)*16);
	memset(buf,'\0',sizeof(char)*16);

	//printf("%p\n%p\n",the_time,tm_ptr);

	time(the_time);
	//tm_hms=gmtime(the_time);
	localtime_r(the_time,&tm_hms);
	//memcpy(tm_hms,localtime(the_time),sizeof(struct tm));
	strftime(buf,16,"%H:%M:%S",&tm_hms);
	free(the_time);
	/*
	 * Looks like struct TM structure can't seem to free
	 */
	//free(tm_ptr);
	return buf;
}

These two functions will often call in the program, but whether to call the function, when calling another program error, immediately launched. In the online to check, is due to the Localtime and gmtime repeat call problem. In the next would like to ask you, have what method can get the system time, C language

Started by Cedric at November 12, 2016 - 9:28 PM

I don't have to run the code, I probably looked at it, I suggest in the function outside malloc you buf. Don't malloc in the function. Mark first, have a look at run time

Posted by Moore at November 18, 2016 - 9:38 PM

I see, you know, don't know not change what you mean:
#include <time.h>
#include <stdio.h>
#include <string.h>
/*
* * Get the local time
* * Returns the string format "_ _ day month year"
* */
void get_local_time_y_m_d(char *buf)
{
time_t *the_time;
struct tm tm_ymd;
the_time=(time_t *)malloc(sizeof(time_t));

time(the_time);
localtime_r(the_time,&tm_ymd);
strftime(buf,16,"%Y_%m_%d",&tm_ymd);

printf("a time = %s\n",buf);
free(the_time);
}
/*
* Get the local time
* Returns the string format "_ divided _ seconds"
*/
void get_local_time_h_m_s(char *buf)
{
time_t *the_time;
struct tm tm_hms;
the_time=(time_t *)malloc(sizeof(time_t));

time(the_time);

localtime_r(the_time,&tm_hms);

strftime(buf,16,"%H:%M:%S",&tm_hms);
printf("b time = %s\n",buf);

free(the_time);

}

int main()
{

char *buf = malloc(16);
memset(buf,0x00,16);

while (1)
{
get_local_time_h_m_s(buf);
get_local_time_y_m_d(buf);
}
free(buf);
}

How to run no problem, your program error, there may be other places, don't stare at this go.

Posted by Moore at November 25, 2016 - 9:47 PM