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?
2 Answers 2
Because it can't.
How would it return a fully typed pointer? C doesn't have templates/generics, and it doesn't allow function overloading; let alone overloading by return type only. So there's no mechanism to have a malloc
that knew what type you wanted.
I've read here that in older C compilers the type of pointer returned by these functions was char* not void*. Is it true?
In C, there is no byte
. char
is guaranteed to be a byte long, so char*
is canonical "a pointer to some byte buffer", which is exactly what memory allocation functions want to return.
-
1conceptually, the returned value from malloc is a char pointer. However, that creates problems when needing to assign that pointer to some other type, such as a struct pointer. 'void', a relatively new keyword, eliminates the need for casting (and makes casting error prone, especially when performing maintenance on the code)user3629249– user36292492015年07月22日 20:17:34 +00:00Commented Jul 22, 2015 at 20:17
-
1@user3629249: No, conceptually it's a pointer to a new uninitialized memory-block, and you haven't decided what it should contain.Deduplicator– Deduplicator2015年07月22日 22:31:32 +00:00Commented Jul 22, 2015 at 22:31
-
1It might be worthwhile to add that
malloc
returnedchar*
in the time beforevoid*
were invented. Oncevoid*
existed, the signature ofmalloc
was quickly adjusted to follow suit.Bart van Ingen Schenau– Bart van Ingen Schenau2015年07月26日 08:57:53 +00:00Commented Jul 26, 2015 at 8:57 -
@BartvanIngenSchenau: It's too bad that C reused the "void" term from functions rather than adding an "untyped memory chunk" type which would allow the free casting of void*, but could also be read and written like "char*". That would allow code to exploit platforms whose minimal addressable unit is smaller than 8 bits and avoid the need to cast untyped memory pointers to char* before using them.supercat– supercat2016年01月29日 23:16:04 +00:00Commented Jan 29, 2016 at 23:16
Of course malloc()
could return a fully typed pointer. The problem is, of what type? If it returned an int*
, it would make you happy, but it would not make me happy, because I want a char*
instead. Clearly, we could not both have it our way. So, void*
is a good baseline which does not take anyone's side.
malloc
returnedint*
and you allocate only 1 byte, you would never be able to dereference it safely without casting.