return time_t错误

时间:2011-12-27 13:37:25

标签: c

程序将返回一组字符串“This is James:00:00:00”和一个时间格式但它崩溃了。我相信内存分配不足但无法确定错误的位置。

FREObject result = 0;

uint32_t len = -1;
const uint8_t *str = 0;
char *temp;
uint8_t *strAll;

time_t curtime;
struct tm *loctime;

/* Get the current time. */
curtime = time(NULL);

/* Convert it to local time representation. */
loctime = localtime(&curtime);

//Turn our actionscrpt code into native code.
if(FREGetObjectAsUTF8(argv[0], &len, &str) == FRE_OK) {
    temp = "Hello World! This is ";

    strAll = (char *)malloc(sizeof(temp) + sizeof(str) + sizeof(loctime));
    strcpy(strAll,temp);
    strcat(strAll,str);
    strcat(strAll,asctime(loctime));
}

1 个答案:

答案 0 :(得分:3)

您可能需要strlen而不是sizeof

strAll = (char *)malloc(sizeof(temp) + sizeof(str) + sizeof(loctime));

sizeof(loctime)也没什么意义。您可能希望将其替换为asctime(loctime)的长度。

也许是这样的:

char *asc = asctime(loctime);
strAll = malloc(strlen(temp) + strlen(str) + stren(asc) + 1);