6

Hi after going through answer here

1: Do I cast the result of malloc? I understood that one of the reason why we do not cast malloc is that

casting malloc is redundant

But what I am still trying to figure out is the warning that will be suppressed when we do cast the malloc function

I also read this answer but I have the follwing doubts

#include<stdio.h>
main()
{
    int *a=malloc(20);
}

I understood the point in the answer that compiler will think that malloc returns an int while we are trying to give that value to an int * which will gives us error cannot convert from int * to int or something like that but the basic question is

Won't the compiler in absence of stdlib.h treat malloc as a user defined function and wont it look for the declaration of it and it will give some error related to missing delcaration/defination

Community
  • 1
  • 1
  • 2
    I get: `warning C4013: 'malloc' undefined; assuming extern returning int` and then `warning C4047: 'initializing' : 'int *' differs in levels of indirection from 'int'`. Perhaps your compiler configuration suppresses those warnings. – barak manos Jun 09 '15 at 05:17
  • @Gopi: Not sure why you deleted your answer, but what you've described is exactly what's happening. – barak manos Jun 09 '15 at 05:19
  • In the absence of corresponding function declarations, compilers will assume that any function is an external function returning an int. This is not limited to malloc(). By not casting malloc() return value, the compiler will detect anomaly when you attempt to assign an int to any pointer type. – alvits Jun 09 '15 at 05:20
  • @barakmanos I am back with my answer. Got confused of why it was down-voted so deleted it take a look at it again – Gopi Jun 09 '15 at 05:21
  • @Gopi: Somebody down-voted the other answer here is well. That's how this website works sometimes (down-votes with no comments to back them up or explain them). Not a reason to delete an answer which is essentially correct. I've up-voted it to compensate. – barak manos Jun 09 '15 at 05:23
  • @barakmanos Thanks sir.. Actually just wanted to relook at the asnwer that's it – Gopi Jun 09 '15 at 05:24
  • @alvits **In the absence of corresponding function declarations, compilers will assume that any function is an external function returning an int** how come the compiler will asume that there is no use of extern keyword with the function malloc ? So what if i exclude the stdlo.h and write a printf in my code ? –  Jun 09 '15 at 05:24
  • @Rohit Saluja: What exactly does `extern` keyword has to do with it? What do you mean by your last comment? – AnT stands with Russia Jun 09 '15 at 05:25
  • 1
    @Rohit Saluja: If you exclude `stdio.h` and call `printf` your code will still compile perfectly fine in pre-C99 compiler. However, `printf` is a special case. It is a variadic function. Since the first standard C version, calling a variadic function without declaring it is *undefined behavior*. Note that it is it is not an "error", it is *undefined behavior*. – AnT stands with Russia Jun 09 '15 at 05:26
  • as alvits pointed out that compiler will assume any function is an external when it is not declared ? But how does it can assume that ? AFAIK it only compiler knows that a function is external when it is declared as exrern in the file where we are calling that function –  Jun 09 '15 at 05:28
  • @Rohit Saluja: Firstly, all functions in C have external linkage by default. There's no need to declare it with an explicit `extern` keyword. If it is not `static`, then it is assumed to have external linkage. Secondly, as for "how can it assume"... It assumes because the language standard C89/90 says so. If you call a completely unknown (undeclared) function it is assumed to be a function with external linkage that returns an `int`. End of story. There's no other "how" here. – AnT stands with Russia Jun 09 '15 at 05:30

2 Answers2

11

In the original C language - C89/90 - calling an undeclared function is not an error. For this reason, a pre-C99 compiler will not produce any "error" due to a missing function declaration. The compiler will simply assume that function returns an int.

It will also automatically and quietly "guess" (infer, derive) the function parameter types from the argument types you supplied in your call. In your example, you supplied 20, which will make the compiler to guess that the "unknown" malloc function takes a single parameter of type int. Note that this is also incorrect, because the real malloc takes a size_t parameter.

In C99 and later the function declaration is required. Which means that forgetting to declare malloc (e.g. forgetting to include <stdlib.h>) is indeed an error, which will result in a diagnostic message. (The parameter-guessing behavior is still there in the language though.)

Note also that in C99 and later declaring function main without an explicit return type int is illegal. The "implicit int" rule is specific to the original version of C language specification only. It no longer exists in C99 and later. You have to declare it as int main(... explicitly.

AnT stands with Russia
  • 312,472
  • 42
  • 525
  • 765
  • 1
    Worth mentioning that some common compilers (e.g. gcc) default to a flavour of C89 mode, and it seems many people stick with the default; so this issue can't just be dismissed by saying "nobody uses C89 anymore". – M.M Jun 09 '15 at 05:36
  • I just had some great fun with this and was quite stunned when I found out. Can anybody tell me how I can force C99 for cl.exe on command line? – iko79 Jul 17 '20 at 15:03
4

In the absence of stdlib.h the compiler thinks that the malloc() function will return int(For C89/90 and not from c99) and you are trying to assign that value to int * and hence there is a type mismatch and the compiler will report it

Gopi
  • 19,784
  • 4
  • 24
  • 36
  • I understood that fact but What i am asking is what about about compiler giving me an error about missing defination of the function malloc –  Jun 09 '15 at 05:19
  • @RohitSaluja Any missing function definition will not be reported and internally compiler will treat it as a func returing `int` – Gopi Jun 09 '15 at 05:22
  • **Any missing function definition will not be reported and internally compiler will treat it as a func returing int** C89/90 rt ? –  Jun 09 '15 at 05:30
  • @RohitSaluja Yeah you are right , it looks like you are on C89/90 and hence it compiles fine and reports the type mismatch error – Gopi Jun 09 '15 at 05:32
  • So for the C89/90 standard will get any warnings when don't inlucde stdlib.h ? –  Jun 09 '15 at 05:40
  • @RohitSaluja Since the standard says what I have put in the answer we will not see any warnings for missing definitions.. I don't know if there is a way to make this condition to throw warning – Gopi Jun 09 '15 at 05:43
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/80012/discussion-between-rohit-saluja-and-gopi). –  Jun 09 '15 at 05:46