Pergunta

Here are the two different versions:

Node *pointer = (Node*)malloc(sizeof(Node));
pointer = NULL;

.

Node *pointer = NULL;

The reason I ask is because I see the first version everywhere in this code I'm working on but I thought it'd be the same as the second. Sometimes instead of being assigned to NULL it's assigned to another variable but the principle is the same, no?

Foi útil?

Solução

They are very different. In the first case you create a Node on the heap and then straight away "forget" it, causing a memory leak. The second case doesn't create a Node and so no leak.

If you really see the first version everywhere in your code, I'd be pretty worried!

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top