Domanda

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?

È stato utile?

Soluzione

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!

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top