+91 9945200008     support@uttaracomputers.com
LOGIN     REGISTER    

malloc returning char * (Audio 27, 50:00)

0
0

Hello sir,

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..

Regards

Keshav

  • You must to post comments
0
0

Thank you.

  • You must to post comments
0
0

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.

 

  • You must to post comments
Showing 2 results
Your Answer

Please first to submit.