這兩種方法都能正常工作,但在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