【发布时间】:2011-03-26 15:36:17
【问题描述】:
可能的重复:
Do I cast the result of malloc?
Should I explicitly cast malloc()'s return value?
你好,
gcc 4.4.4 c89
通常我不会从 malloc 调用中转换返回结果。
int *int_ptr = NULL;
int_ptr = malloc(sizeof(int));
但是,我在这里读到过,如果你施放它可以隐藏错误。如果显式转换为 int,它如何隐藏错误?
int_ptr = (int*)malloc(sizeof(int));
另外,我正在阅读一本 c 编程书籍,它说从 void 指针(包括来自 malloc 的调用)进行强制转换是一种很好的编程习惯。
什么是好的编程习惯?
int *int_ptr = NULL;
void *ptr = NULL;
int_ptr = ptr;
或
int_ptr = (int*)ptr;
非常感谢您的建议,
【问题讨论】: