Coming from Java I'm confused by the use of Void allowing a return value in the following:
void *emalloc(size_t s) {
void *result = malloc(s);
if (NULL == result) {
fprintf(stderr, "MEMORY ALLOCATION FAILURE\n");
exit( EXIT_FAILURE );
}
return result;
}
Is this returning a pointer to a chuck of allocated of memory ?
Yes, it is. A void* pointer is basically a generic pointer to a memory address, which could then typically be typecast to whatever type was actually desired.
Your question indicates that you are misreading the function's return type. There is a big difference between:
void foo( void ) {}
and
void *bar( void ) {}
foo() takes no arguments and does not return a value, while bar() takes no arguments and returns a generic pointer. In C, the keyword void is used to indicate a generic pointer, and an object of type void * can be converted to any other object pointer type without loss of information.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With