5

How i think i write code

void safeFree(void *ptr){
if(ptr != nullptr){
delete(ptr);
}
}

How i actually write code

void safeFree(void *ptr){
*nullptr = 7; //TODO
}

Comments
  • 1
    Doesn't compile, nullptr is a keyword since C++11, so assigning it a value will get you a compiler error.

    And checking a pointer against any variant of NULL before deleting it, is completely pointless. That's already done by 'delete'. No need to do so, although it's a frequent sighting in the wild.

    You'd better assign NULL to you pointer after deletion to avoid acces to freed memory and deletion of already freed memory.
Add Comment