C++: delete vs. free and performance

后端 未结 7 1276
天命终不由人
天命终不由人 2021-01-30 11:07
  1. Consider:

    char *p=NULL;
    free(p) // or
    delete p;
    

    What will happen if I use free and delete on p?

7条回答
  •  走了就别回头了
    2021-01-30 11:38

    Answer 1: Both free(p) and delete p work fine with a NULL pointer.

    Answer 2: Impossible to answer without seeing the slow parts of the code. You should profile the code! If you are using Linux I suggest using Callgrind (part of Valgrind) to find out what parts of the execution takes the most time.

提交回复
热议问题