# Dangling pointer（懸置指標）

在C語言中，如果一個指標不再使用了，那麼我們應該要在釋放指標所指的記憶體之後，將指標指定為空值（NULL）。

```c
#include <iostream>
#include <string.h>
#include <stdio.h>
#include <stdlib.h> 
 
using namespace std;
int main(int argc, char * argv[])
{
     char *cmd = NULL;

     cmd = new char[16];
     printf("cmd: %p\n", cmd);

     if(cmd) {
         delete [] cmd;
     }

     printf("delete cmd: %p\n", cmd);

     cmd = (char*)malloc(sizeof(char)*16);

     printf("cmd: %p\n", cmd);
     if( cmd ) {
         free(cmd);
     }
     printf("free cmd: %p\n", cmd);

     return 0;
}
```

\
myliao\@my-thinkpad:/tmp$ g++ play.cpp -o play\
myliao\@my-thinkpad:/tmp$ ./play\
cmd: 0x9943008\
delete cmd: 0x9943008\
cmd: 0x9943008\
free cmd: 0x9943008\
\
從程式的結果來看，可以知道，我們 free cmd 之後，cmd 指標並不會自動改為NULL。\
\
這會有什麼問題呢？\
\
如果說你的指標是要重複使用的，如果沒有在 free() 之後，手動將指標修改為 NULL，則之後的程式碼會無法得知該指標所指向的記憶體已經被釋放了。


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://applezulab.netdpi.net/linux-prog/dangling-pointer-xuan-zhi-zhi-biao.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
