문제

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?

도움이 되었습니까?

해결책

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!

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top