程序将返回一组字符串“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));
}

最佳答案

您可能需要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);

关于c - 返回time_t错误,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/8645241/

10-11 18:53