4

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?

4
  • possible duplicate of Why does void in C mean not void?
    – gnat
    Commented Jul 22, 2015 at 18:50
  • 1
    This is because these methods are allocating memory without any specific type. If malloc returned int* and you allocate only 1 byte, you would never be able to dereference it safely without casting.
    – Matthew
    Commented Jul 22, 2015 at 18:52
  • in C, it is a bad idea to have any casting at all when assigning the returned pointer from malloc (and family of functions) Commented Jul 22, 2015 at 20:13
  • 1
    Questions are related but I am not convinced this is a duplicate. Having written the accepted answer on the dupe target, it would add insight to this question but I do not believe it would answer it.
    – user22815
    Commented Jul 23, 2015 at 10:37

2 Answers 2

10

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.

4
  • 1
    conceptually, 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) Commented 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. Commented Jul 22, 2015 at 22:31
  • 1
    It might be worthwhile to add that malloc returned char* in the time before void* were invented. Once void* existed, the signature of malloc was quickly adjusted to follow suit. Commented 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
    Commented Jan 29, 2016 at 23:16
2

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.