这两种方法都能正常工作,但在ptr == NULL
的情况下哪一种更快/性能更好?免费(指针)方法的性能?
void voo()
{
str *ptr = NULL;
// try to malloc memory and do something
// leaving methode and free the memory
if(ptr != NULL)
{
free(ptr);
ptr = NULL;
}
}
如果我离开该方法,是否需要if
查询?在任何情况下给free
内存不是一样快吗?
void baa()
{
str *ptr = NULL;
// try to malloc memory and do something
// leaving methode and free the memory
free(ptr);
ptr = NULL;
}
'免费(ptr)'你的意思是吧? – 2014-09-04 12:02:35
为什么要标注C++?您通常不会在C++中使用'malloc' /'free',并且在大多数情况下,您不会使用动态分配。 – crashmstr 2014-09-04 12:04:29
最后的'ptr = NULL'也是没有意义的(尽管编译器应该认识到这一点,并且不会为它产生任何浪费时间的代码)。 – 2014-09-04 12:10:30