2

Earlier I asked one question that is if the return of malloc is cast then the error which would be flagged is hidden and I got the answer that is: In (older dialects of) C, you can call a function which hasn't been declared; that implicitly declares a function with a return type of int. So, if you were to call malloc() without including the header, you'd get code that erroneously assumed that it returned int. Without a cast, you'd get a compiler error when you tried to assign it to a pointer. With a cast, the code would compile, potentially giving obscure runtime errors and lengthy debugging sessions.

I understand that without inclusion of <stdlib.h> header file compiler implicitly declares a function with a return type of int.

But I confused in that according to malloc()'s function definition that is defined by creator, it will returns void * whenever we used it, but without <stdlib.h> it returns int. So how does its definition change, is compiler implicitly changing void * to int * type, or there is some other reason?

I know that I couldn't explain my question properly but if anybody understand, please explain me about that.

Community
  • 1
  • 1
Vikas Verma
  • 3,626
  • 6
  • 27
  • 40
  • 1
    The `malloc` function doesn't return `void`, it returns a `void` *pointer* (i.e. `void *`). – Some programmer dude Jan 24 '14 at 08:35
  • @JoachimPileborg OP knows that, but he uses the word **address** instead of **pointer** which is not a good idea. – Yu Hao Jan 24 '14 at 08:37
  • 1
    @YuHao On the contrary, it is a very good idea. Beginners who believe that a pointer is something magical, instead of just a number like any other variable, will have a hard time understanding pointers. – Lundin Jan 24 '14 at 09:06
  • Very related: [Do I cast the result of malloc?](https://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc) (TL;DR: No). – Some programmer dude Jan 21 '20 at 14:44

2 Answers2

12

For C, all undeclared functions are implicitly declared to return int and take an unspecified number of unspecified arguments.

The problem with malloc is that an int and a void * may not be the same size, or even compatible. For example, on modern 64-bit machines pointers are 64-bit types while int is a 32-bit type, so if you don't have a proper declaration of malloc the compiler will drop the high 32 bits of the pointer.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
1

In standard C, this will never happen. If you forget to include <stdlib.h> on a standard compiler and then call malloc, you will get a compiler error "implicit declaration" or similar, because the function has no prototype.

In older, obsolete versions of the C standard, there was a rule that said that if no prototype of a called function was visible, the return type of the function was assumed to be int. This was of course very dangerous and lead to all kind of obscure bugs. Therefore, the implicit int "feature" was removed from the C language 15 years ago, when the standard C99 was released.

Lundin
  • 195,001
  • 40
  • 254
  • 396
  • And thus the old debate "never cast the result of malloc" is kind of obsolete. Casting the result of malloc when you forget to include stdlib.h will not result in severe bugs on a modern C compiler. But of course, it is still completely pointless to cast the result. – Lundin Jan 24 '14 at 09:09