Just curious why malloc used to return ‘char *’ by default when the return value is an address to <data type including non-chars>.
I see these days `man malloc` (and the variants, such as calloc, etc) on linux and osx shows ‘void *’ as the return value, which seems appropriate?
Perhaps there’s a historical reason for the ‘char *’ return value? Just curious..
Dear Keshav, your guess is correct. It is a historic reason. Please remember that the course is UNIX internals and not Linux or any other variant. In fact, most of the lectures you hear were delivered before Linux was released into the market. (void *) is indeed the appropriate type for malloc to return since it will be autocast to any other type of address by the compiler. For example,
int * x;
stuct complicated * y;
x = malloc ( sizeof(int) );
y = malloc ( sizeof * y );
will work perfectly.
Please login first to submit.