I need a general way to check if void * 0 contains up to num_bytes. I came up with the following approach. * p does not contain the same data type every time, therefore can not do*(type*)p
bool is_pointer_0(void *p, int num) {
void *cmp;
cmp = (void*)malloc(num);
memset(cmp, 0, num);
if (memcmp(p, cmp, num)) {
free(cmp);
return false;
} else {
free(cmp);
return true;
}
}
The function allocates and frees num bytes on every call, but not really I think. Please suggest faster approaches. Appreciate the help.
Update:
How about this approach?
bool is_pointer_0(void *p, int num) {
void *a = NULL;
memcpy(&a, p, num);
return a == NULL;
}
source
share