> For the complete documentation index, see [llms.txt](https://mentebinaria.gitbook.io/manual-da-linguagem-gnu-c/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://mentebinaria.gitbook.io/manual-da-linguagem-gnu-c/14.-ponteiros/desreferenciando-ponteiros-nulos-ou-invalidos.md).

# Desreferenciando Ponteiros Nulos ou Inválidos

Tentar desreferenciar um **ponteiro nulo** é um erro. Na maioria das plataformas, isso geralmente causa um **sinal**, normalmente `SIGSEGV` (veja [*Sinais*](#user-content-fn-1)[^1]).

```c
char *foo = NULL;
c = *foo;    /* Isso causa um sinal e termina o programa. */
```

O mesmo ocorre com um ponteiro que possui **alinhamento incorreto** para o tipo de dado apontado (em muitos tipos de computadores), ou que aponta para uma parte da memória **não alocada** no espaço de endereçamento do processo.

Esse sinal termina o programa, **a menos que** ele tenha sido configurado para lidar com o sinal (veja [*The GNU C Library*](https://www.gnu.org/software/libc/manual/html_node/Signal-Handling.html#Signal-Handling) em *The GNU C Library Reference Manual*).

No entanto, o sinal pode **não ocorrer** se a desreferência for **eliminada pela otimização**. No exemplo acima, se você não usar posteriormente o valor de `c`, o GCC pode otimizar e remover o código de `*foo`. Para evitar esse tipo de otimização, você pode usar o qualificador `volatile`, como neste exemplo:

```c
volatile char *p;
volatile char c;
c = *p;
```

Você pode usar isso para testar se `p` aponta para memória não alocada. Basta configurar primeiro um **tratador de sinal**, para que o sinal não termine o programa.

[^1]: Apêndice pendente de tradução


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## 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, and the optional `goal` query parameter:

```
GET https://mentebinaria.gitbook.io/manual-da-linguagem-gnu-c/14.-ponteiros/desreferenciando-ponteiros-nulos-ou-invalidos.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

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.
