c - 返回 time_t 错误

标签 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));
}

最佳答案

你可能需要 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/

相关文章:

c - GTk 每个小部件加速器

c++ - calloc 比 malloc 好吗?

c - 如何从 C 数组中删除重复的字符串?

c - merge_sort() 中的 merge() 如何工作?

c - 调用进程的符号查找?

c - 除了这个 if 语句之外,还有有效的发牌方法吗?

c - 用 C 打开 LDAP 客户端 - 如何替换已弃用的 API

c - C 形式定义中的命名空间

c - 打印多维字符数组

c - 动态内存访问仅在函数内部有效