Consider the prototypes of C's dynamic allocation functions
malloc - void* malloc(size_t size);
calloc - void* calloc(size_t n,size);
realloc - void* realloc(void* ptr,size_t newsize);
Now a statement like following
int* p=(int*)malloc(sizeof(int));
I know that casting to (int*) isn't needed in C since a void pointer can be assigned to a pointer variable of any object type, but why these functions by default not return fully typed pointer?. Wouln't it be nice if it returns int* instead of void pointer in this case? What is the reason void* is choosen as return type of these functions?
One other question: I've read here that in older C compilers the type of pointer returned by these functions was char* not void*. Is it true?
malloc
returnedint*
and you allocate only 1 byte, you would never be able to dereference it safely without casting.