Reputation:
I came across this code implementing a queue
typedef struct Node {
void * data;
struct Node * next;
} node_t;
the node is free'd in this way
static void freeNode(node_t *node) { free((void*)node); }
Why is the node casted to void *
?
Should't have been just free(node)
?
And since I'm working on another structure :
typedef struct TaskNode {
void (* fun)(void *);
void * arg;
struct Task * next;
} task_t;
Should I cast the pointer to the malloc'd structure to void *
too before freeing it?
Upvotes: 0
Views: 45
Reputation: 45654
You are quite correct that casting to void*
is gratuitious. And as it doesn't clarify anything for fellow programmers either, it shouldn't be there.
Naturally, that's the case for both examples.
Upvotes: 1
Reputation: 26717
The cast is useless in this context, c auto-promote any pointer to void *
in a lot of context(not for stdarg for exemple):
free((void*)node);
is equivalent to:
free(node);
Upvotes: 1