如何将free()紧接着分配NULL导致悬空指针?

ckv*_*ckv 2 c++ pointers

下面的代码如何导致悬空指针.

{
    char *cPointer = malloc ( some constant number );
    /* some code */
    free ( cPointer );      
    cPointer = NULL;        
    /* some code */
}
Run Code Online (Sandbox Code Playgroud)

ste*_*anB 14

它不能.

这个会:

char * a = malloc(556);
char * b = a;
free(a);
a = NULL;
Run Code Online (Sandbox Code Playgroud)

b现在是悬空指针,因为它指向它的对象已经消失,但b仍然将地址存储到以前对象的内存中,当您尝试访问它时会得到有趣的结果 - 这取决于内存是否已被重用或未被触及.